Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"SELECT * FROM view_table WHERE cause" => But the result always reorder

Tags:

mysql

In SELECT * FROM view_table The result always is

-----------------------
|| id  ||   author   ||
-----------------------
||  1  ||    a       || <--
||  1  ||    c       || <--
||  1  ||    b       || <--
||  2  ||    d       ||
||  3  ||    e       ||

But when SELECT * FROM view_table WHERE id=1 the result is

-----------------------
|| id  ||   author   ||
-----------------------
||  1  ||    a       ||
||  1  ||    b       ||
||  1  ||    c       ||

Or

-----------------------
|| id  ||   author   ||
-----------------------
||  1  ||    b       ||
||  1  ||    c       ||
||  1  ||    a       ||

Or

-----------------------
|| id  ||   author   ||
-----------------------
||  1  ||    a       ||
||  1  ||    c       ||
||  1  ||    b       ||

Or

..

I want to SELECT * FROM view_table WHERE id=1 and get the results in the same order as SELECT * FROM view_table. How to solve it

Thank You.

like image 439
Doggo Avatar asked Jan 21 '26 03:01

Doggo


1 Answers

The order of results is never guaranteed unless you use an ORDER BY clause. How you want to order it, is up to you.

For example,

SELECT * 
FROM view_table 
WHERE id = 1
ORDER BY author

This will sort it based on the author's name (from A to Z, or Z to A if you use ORDER BY author DESC).

Alternatively you can add a Created column and then order by that column instead, which holds the timestamp when it was created. That could mimic an order that you want to keep.

like image 111
Qirel Avatar answered Jan 22 '26 20:01

Qirel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!