eMythMakers.com
eMythMakers.com

Reputation: 19

Fix MySQL query syntax error (LIMIT)

I have a table 'tbl_content'

When I run the following query I can see 11 records/data

SELECT ContentID, CategoryID, Home, Inner
FROM tbl_content
WHERE CategoryID = 1 AND Inner = 2
ORDER BY ContentID DESC

When I run the following query I can see first 4 records/data

SELECT ContentID, CategoryID, Home, Inner
FROM tbl_content
WHERE CategoryID = 1 AND Inner = 2
ORDER BY ContentID DESC LIMIT 7, 10

I want to update value of the remaining first 4 records/data. But unfortunately it is generating error.

UPDATE tbl_content
SET Inner = 1
WHERE CategoryID = 1 AND Inner = 2
ORDER BY ContentID DESC LIMIT 7, 10

Can anyone kindly give any solution?

Upvotes: 0

Views: 289

Answers (2)

ScaisEdge
ScaisEdge

Reputation: 133380

You could try uisng an IN clause for subslect with limit

UPDATE tbl_content SET Inner=1 
WHERE CategoryID=1 AND Inner=2 
AND ContentID  IN ( 
 select contentID from (
  select ContentID  
  from  tbl_content 
  order by ORDER BY ContentID DESC LIMIT 7, 10 ) t

)

or a join

UPDATE tbl_content c 
INNER JOIN (
  select ContentID  
  from  tbl_content 
  order by ORDER BY ContentID DESC LIMIT 7, 10 
) t  on t.ContentID = c.ContentID
SET c.Inner=1 
WHERE c.CategoryID=1 AND c.Inner=2 

Upvotes: 4

maziyank
maziyank

Reputation: 616

try this.

UPDATE tbl_content SET Inner=1 WHERE id IN(SELECT ContentId FROM (SELECT ContentID, CategoryID, Home, Inner FROM tbl_content WHERE CategoryID=1 AND Inner=2 ORDER BY ContentID DESC LIMIT 7, 10));

Upvotes: 1

Related Questions