0

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?

Dharman
  • 30,962
  • 25
  • 85
  • 135

2 Answers2

4

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 
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • @TimBiegeleisen .. probably you are right .. anyway wrapped the subquery for avoid problem in the IN sample .. thanks – ScaisEdge Aug 27 '18 at 14:43
  • Your first solution updates records from number 5th to 8th. And your last solution updates first 4 records. But I wanted to keep first 7 records intact and change last /rest of the records. – eMythMakers.com Aug 29 '18 at 13:10
1

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));
maziyank
  • 581
  • 2
  • 10