SQL Switching output order - php

I couldn't find this anywhere and I am having a hard time to fix this.
Here is my SQL query:
SELECT id, subject, content, description, date, image
FROM articles
WHERE id > $lastid
ORDER BY id ASC
LIMIT 5
This give me output in a following order id 11,12,13,14,15.
Is there a way to switch this output so it would be id 15,14,13,12,11?

SELECT * FROM (
SELECT id, subject, content, description, date, image FROM articles WHERE id > $lastid
ORDER BY id ASC
LIMIT 5
) t
ORDER BY id DESC
The documentations tells you that there are two ways to sort: ascending and descending. The corresponding sql elements are:
ORDER BY [Field] ASC for ascending and
ORDER BY [Field] DESC for descending.

Related

MYSQL query code to check if statement and get row values

so I was trying to use if statement on MySQL. I have a table contains tp1 column and in this column I have two different string values L1_ON or L1_OFF, I want to get L1_ON if there is one on tp1 column but if there is no L1_ON just get L1_OFF. this is what wrote and i know it's not correct just for extra explanation.
$query1 = sprintf('SELECT id,dateandtime,tp1 FROM plate WHERE AND IF(tp1 LIKE "L1_ON") else (tp1 LIKE "L1_OFF") ORDER BY id ASC LIMIT 1');
If "L1_ON" and "L1_OFF" are the only possible values, then you could just sort by tp1 column to ensure "L1_ON" columns come up first:
SELECT
id,dateandtime,tp1
FROM plate
ORDER BY tp1 DESC, id ASC
LIMIT 1
If there could be more than those two values, and you're using this in a script, honestly I would probably just run two queries. One trying to get "L1_ON" values, then a second getting all other values.
SELECT
id,dateandtime,tp1
FROM plate
WHERE tp1 = "L1_ON"
ORDER BY id ASC
LIMIT 1
Finally, if you really want to do this in one query, you could union two queries, one for all "L1_ON" values, followed by one for any that are NOT "L1_ON"
SELECT *
FROM (
SELECT
id,
dateandtime,
tp1
FROM plate
WHERE tp1 = "L1_ON"
ORDER BY id ASC
) AS tp1_on
UNION ALL
SELECT *
FROM (
SELECT
id,
dateandtime,
tp1
FROM plate
WHERE tp1 != "L1_ON"
ORDER BY id ASC
) AS tp1_off
LIMIT 1
using if statement I found the best answer to my question like following:
$query = sprintf('SELECT IF (tp1 REGEXP "L1_ON" ,dateandtime ,"L1_OFF") id,dateandtime,tp1 FROM plate ORDER BY id ASC LIMIT 1 ');
so the code check if tp1 has L1_ON then give me time for that raw, if not just give any L1_OFF raw with time (dateandtime= time of L1_.. raw).

mysql: order the result by column name

$qry="select * from table where category='car' or title='car' or description='car'";
but I want the output to list the rows by category first and then title and then description.
****Edit: actually I am using a like operator to search**
Example:
id category title description
1 car
2 car
3 car
4 car
5 car
6 car
is there any way other than using union?
Thanks in advance.
You can do this using ORDER BY with the right keys. In MySQL, you can do:
ORDER BY (category = 'car') DESC,
(title = 'car') DESC,
(description = 'car') DESC
MySQL treats boolean expressions as integers in a numeric context, with 0 for false and 1 for true. So the DESC puts the true versions first.
You can also simplify the WHERE clause if you like:
WHERE 'car' IN (category, title, description)
You can get this result by using this statement:
SELECT * FROM mytable
WHERE (category='car' OR title='car' OR description='car')
ORDER BY category = 'car' DESC,
title = 'car' DESC,
description = 'car' DESC
How it works?
It will set the orders of data in DESC by sequentially as mentioned in query. You can change the sequence as you want.
Try this ,
SELECT * FROM table where 'car' IN (category, title, description) ORDER BY category DESC, title DESC, description DESC
You can use ORDER BY for multiple columns like:
SELECT * FROM tablename ORDER BY category DESC, title DESC, description DESC
I have tried it and it worked.
You can have multiple ORDER BY clause in your query.
select *from table where category='car' or title='car' or description='car' ORDER BY category DESC, title DESC, description DESC
See this answer for reference. mysql query order by multiple items
Try this Query :
select * from table
where category='car' or title='car' or description='car'
order by 1 desc, 2 desc, 3 desc

MySQL query multiple column ordering

So, I've written a query which should grab 15 most recent results from the 'messages' table, but order results by date in descending direction. My current query is as follows:
SELECT * FROM messages
WHERE chatID = 1
ORDER BY ID DESC, timeSent ASC
LIMIT 15
As you can see, I am using the 'ID DESC' to get the 15 most recent results, but the 'timeSent ASC' isn't ordering the results in the order I wish.
How can I correct my query to achieve this?
First fetch the messages by ordering the ID then sort it according to timeSent. You can try this -
SELECT * FROM
(SELECT * FROM messages WHERE chatID = 1 ORDER BY ID DESC LIMIT 15) messages_ordered
ORDER BY timeSent ASC

How to reverse order output of a MySQL query

I have a basic write to and retrieve SQL database PHP "thing" and I want to have the output in descending order. How do I do that?
For example, the last entry is shown first, then the entry before that, then the entry before that, etc. The first entry ever is last.
Use:
SELECT field_name
FROM table_name
ORDER BY id DESC
By default, MySQL will show results in ascending order. If you want to show them in reverse order, use ORDER BY field_name DESC.
You can use id or date as the field name.
Change the ORDER BY statement
from ORDER BY col or ORDER BY col ASC or to ORDER BY col DESC
from ORDER BY col DESC to ORDER BY col ASC
Sort using DESC ORDER BY.
SELECT * FROM <TABLE> ORDER BY <COLUMN> DESC
If there's an auto increment field, you order by that field, descending.
SELECT "column_name"
FROM "table_name"
[WHERE "condition"]
ORDER BY "column_name" [ASC, DESC];
That's from ORDER BY Clause - Sort Data In SQL.
Write the below query to retrieve data:
SELECT * FROM `table_name` order by id desc
It will retrieve data from the table in descending order.
MySQL general query syntax for SELECT:
SELECT field1, field2,...fieldN FROM table_name
[WHERE Clause][GROUP BY]
[ORDER BY][LIMIT]
Example query 1:
SELECT id,first_name,last_name,email FROM users ORDER BY first_name desc // In this case you can only fetch data by = id,first_name,last_name,email
Example query 2:
SELECT * FROM users ORDER BY first_name desc // In this case all fields will be selected
Note: ORDER BY ASC = Data will sort by Ascending Order & ORDER BY DESC = Data will sort by descending Order

Select on Mysql inverse order

i have a MySql table that consists of 2 basic things:
The id and a value.
To show that on my page, i need to select, for example, the last 100 rows on reversed order.
So imagine that someone is putting data on it:
Id, value
1, 10
2, 9
3, 21
4, 15
i need, to select the last "3" rows (LIMIT + ORDER Clause), but not like this: 4,3,2 but like this: 2,3,4.
I know how to do that on code, but maybe there is a simple solution for that on Mysql and i don`t know.
Thanks
My SQL Query is like this right now:
SELECT `Data`.`id`, `Data`.`log_id`, `Data`.`value`, `Data`.`created` FROM `control_panel`.`datas` AS `Data` WHERE `Data`.`id` > 1000 AND `Data`.`log_id` = (2) ORDER BY `Data`.`id` DESC LIMIT 100
You need to wrap the first ORDER BY in a subselect which will return a limited selection ordered in descending order, then you can order that result in the outer query in ascending order:
SELECT
a.*
FROM
(
SELECT id, value
FROM tbl
ORDER BY id DESC
LIMIT 3
) a
ORDER BY
a.id
One way to do this would be with a sub-select.
SELECT *
FROM (SELECT * FROM table_name ORDER BY id DESC LIMIT 3) tmp
ORDER BY id ASC
simply
SELECT t.*
(SELECT * FROM table_name
ORDER BY column_name DESC
LIMIT 0,3) t
ORDER BY t.column_name ASC
use DESC to descending order, ASC to increasing order

Categories