1

I have two table dsl_checkins and area_distance. In area_distance 3 rows :

distance
area1
area2

Example:

distance = 3456
area1 = phoenix
area2 = houston

So i need get from dsl_checkins table entry where distance is lowest and need carry about usage_flag field. My idea was use following query:

UPDATE dsl_checkins DC
    join area_distance AD
    on AD.area2 = DC.area
set DC.usage_flag=1,
    DC.id = (SELECT @dsl_id := DC.id)
WHERE DC.active = 1 AND
      DC.offline = 0 AND
      usage_flag = 0 AND
      AD.area1 = 'houston'
ORDER BY AD.distance ASC, RAND()
limit 1;

but i get error:

Error Code: 1221. Incorrect usage of UPDATE and ORDER BY

Any idea how to do it without lock whole dsl_checkins table?

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
comalex3
  • 2,497
  • 4
  • 26
  • 47

1 Answers1

0

Order by is not compatible with Update

UPDATE with ORDER BY and LIMIT not working in MYSQL

Community
  • 1
  • 1
Tibor Szasz
  • 3,028
  • 2
  • 18
  • 23