-2

I have 10 records in mysql and suppose i want that 4th record should be on top and remaining 9 are retrieved after 4th , can anyone please tell me ? I stuck at

Select * from info <what to put here> ??

I tried using this code but

using this code gives an error click here to see

4 Answers4

0
SELECT * FROM info WHERE id = 4
UNION
SELECT * FROM info WHERE id != 4;

Later edit, based on new information

SELECT * FROM profiles WHERE sno = 30
UNION
SELECT * FROM profiles WHERE sno != 30;
Alex
  • 426
  • 3
  • 9
  • UNION will probably reorder the result set. You should at least use UNION ALL. However a specific order is never guaranteed withot an ORDER BY clause. – Paul Spiegel Mar 17 '17 at 14:15
0

Try this one :

select * from info where sno = 4
UNION ALL 
select * from info where sno <> 4

First, I select only the row number 4, then I just add all the remaining rows by using <> which means different of. The UNION ALL do the union between the 2 select

kaldoran
  • 817
  • 6
  • 19
0

If you are sure that you need exactly the 4th element, just get the data from the database:

$sql = "SELECT * FROM info;";

Then while printing print $elemets[3] first (as $elements[0] is the first row that is fetched) and then do unlink($elements[4]); and print the rest of the elements.

Bojan Radaković
  • 360
  • 1
  • 11
0

You can use a boolean expression for the order. sno = 30 will return 1 for that specific row and 0 for all other rows. So you can use it for descending order.

Select * 
from info
order by sno = 30 desc, sno
Paul Spiegel
  • 29,577
  • 5
  • 40
  • 50