-2

I have 10 records in MySQL and suppose I want that 4th record should be on top and the remaining 9 are retrieved after the 4th.

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

I tried using this code but it's wrong

SELECT * FROM profiles WHERE sno = 30 ORDER BY sno UNION ALL SELECT * FROM profiles WHERE sno = 30 ORDER BY sno
Dharman
  • 30,962
  • 25
  • 85
  • 135

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
  • 436
  • 4
  • 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
  • 845
  • 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ć
  • 430
  • 3
  • 12
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
  • 30,925
  • 5
  • 44
  • 53