Mysql selects everything except the first and last line

I want the database to display all rows except the first and last as I have CSS code for them.

I've tried this:

SELECT * FROM db 
WHERE 
keywords LIKE '%example%' LIMIT 9999 OFFSET 1
 AND 
keywords LIKE '%example%' DESC LIMIT 9999 OFFSET 1

      

Since the number of lines can increase, I cannot write the exact number.

+3


source to share


5 answers


There is really no reason to complicate your query by trying to disable these values ​​at the SQL level, you can do:

$results = $db->query( /* regular query with all results */ );

array_pop($results);
array_shift($results);

// Now use $results, which now contains only the "middle" content

      



If you really want it at the DB level, you can use:

SELECT * FROM db 
WHERE keywords LIKE '%example%' 
AND id <> (SELECT MAX(ID) FROM TABLE)
AND id <> (SELECT MIN(ID) FROM TABLE)

      

+4


source


You can try this for example:

SELECT * FROM TABLE WHERE ID != (SELECT MAX(ID) FROM TABLE) LIMIT 1,9844674507370

      



But as I said in the comment: It is highly advisable that you handle this with PHP code to avoid creating two or more requests

0


source


You can use UNION

like

SELECT * FROM db WHERE keywords LIKE '%example%' order by keywords ASC limit 1 UNION SELECT * FROM db WHERE keywords LIKE '%example%' order by keywords DESC limit 1;

      

0


source


1st: Just you can handle this thing in php like below. avoid two requests

 $last_record =count($records)-1;
 $i=0;
   foreach($records as $key=>$row)
   {
      if($i==0){
           //apply the css
       }

      if($i== $last_record ){
           //apply the css
       }
   }

      

request:

SELECT * FROM db WHERE keywords LIKE '%example%' 

      

0


source


You can do this without using LIMIT

ANDOFFSET

SELECT * FROM table_name WHERE id != (SELECT MAX(id) FROM table_name) AND id != (SELECT MIN(id) FROM table_name)

SELECT * FROM db 
WHERE 
keywords LIKE '%example%'
AND
id != (SELECT MAX(id) FROM db) 
AND 
id != (SELECT MIN(id) FROM db)

      

here id will be your auto-increment

0


source







All Articles