mysql: order the result by column name - php

$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

Related

How can I union random selected rows in mysql?

Now I use this code:
$query = mysqli_query($conn, "(SELECT * FROM movies WHERE
MATCH(title) AGAINST('".$_SESSION['mtitle']."' WITH QUERY EXPANSION) AND
id NOT LIKE '".$_SESSION['mid']."')
UNION
(SELECT * FROM movies WHERE category LIKE '%".$cat."%' AND
id NOT LIKE '".$_SESSION['mid']."' ORDER BY RAND()) LIMIT 5");
I would like to list 5 similar movies. When there is 5 or more matches in the first part, it works good. But if isn't, the second selection isn't random, it writes always the same movies in the same sequence.
For example: If I search for Jumanji, the first suggestion is Jumanji, the second is Jumanji: Welcome to the Jungle. But the last 3 is always the same movie.
Group By think be your answer to remove any duplicate values.
`GROUP BY mtitle`
"(SELECT mtitle FROM movies WHERE
MATCH(title) AGAINST('".$_SESSION['mtitle']."' WITH QUERY EXPANSION) AND
id NOT LIKE '".$_SESSION['mid']."')
UNION
(SELECT mtitle FROM movies WHERE category LIKE '%".$cat."%' AND
id NOT LIKE '".$_SESSION['mid']."') GROUP BY mtitle ORDER BY RAND() LIMIT 5"

SQL Switching output order

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.

SQL using less than in to order the result

If there were a table like the follwing:
id title
1 a
2 b
3 c
4 d
I want to know if there is a way of selecting rows in a order by first selecting the rows with id less than 3 ordering them in id descending order, than selecting the rest in id descending order, so in this case, row 2,1,4,3.I was wondering if there was a sql statement something like this:
SELECT * FROM tablename ORDERY BY id<'3' id DESC, id DESC
You can use multiple expressions in the order by:
order by id < 3 desc,
id desc
This is close to your expression, but you have an extra id in it.
It will do the same like Gordon's answer. But I think this will make more sense for understanding.
ORDER BY CASE id < 3 THEN 1, ELSE 0 END DESC,
id DESC

SQL - concatenate results from 2 tables

First I need to get exact match like
SELECT * FROM movies WHERE title='STRING' ORDER BY x DESC
and then append to these results query with LIKE match
SELECT * FROM movies WHERE title LIKE '%STRING&' AND title<>'STRING' ORDER BY x DESC
and limit these results with maximum of 10 results.
UNION wont`t do the jobs as it sorts all results together and returns wrong order (I need exact match first, then with LIKE)
SELECT * FROM movies WHERE title='STRING' UNION
SELECT * FROM movies WHERE title LIKE '%STRING%' ORDER BY x DESC LIMIT 10
The best solution I got is to use multi_query()
$query = "SELECT * FROM movies WHERE title='STRING' ORDER BY x DESC; ";
$query .= "SELECT * FROM movies WHERE title LIKE '%STRING%' AND title<>'red' ORDER BY x DESC";
$Dbi->multi_query($query);
do {
$sql = $Dbi->store_result();
while($x = $sql->fetch_array()) {
...
}
} while($Dbi->next_result());
but in this case it is not possible to use any mysql inside the inner loop and there also must be better looking solution!
You can do this with one query, by using the order by clause:
SELECT *
FROM movies
WHERE title like '%STRING%'
ORDER BY title = 'STRING' desc,
title like '%STRING%' desc
LIMIT 10;
The first clause in the ORDER BY puts the exact matches first. The second then orders by the partial matches. The WHERE clause ensures that there is a match of some kind.
You don't need the UNION, it's accessing the same table twice:
SELECT *
FROM movies
WHERE title LIKE '%STRING&'
ORDER BY CASE WHEN title='STRING' THEN 1 ELSE 2 END
LIMIT 10
(SELECT * FROM movies WHERE title='STRING')
UNION
(SELECT * FROM movies WHERE title LIKE '%STRING%' ORDER BY x DESC LIMIT 10)

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