can order by follow it union select? - mysql

is it possible in an SQL query to add UNION after order by 1
SELECT * FROM table1 WHERE etc='1' ORDER BY 11;
can we add a union select query beside 11 to be like this ?
SELECT * FROM table1 WHERE etc='1' ORDER BY 11 UNION select etc etc etc ...;

In MySQL, you can enclose the order by clause within a subquery and union the results of multiple subqueries; something like this:
SELECT * FROM (SELECT * FROM table1 WHERE etc='1' ORDER BY 11) sq1
UNION ALL
SELECT * FROM (SELECT * FROM table2 WHERE etc='2' ORDER BY 12) sq2
...

Maybe something like this:
SELECT
*,
11 AS orderby
FROM
table1
WHERE
etc='1'
UNION
select
*,
10 AS orderby
FROM
table2
ORDER BY
orderby

You can use like this:
SELECT * FROM table1 WHERE etc='1'
union
SELECT * FROM table1 WHERE etc='1'
ORDER BY any_column

You can only use ORDER BY at the end of the query after the last union. Basically all SELECTs are done first, then the entire result set is ordered.

Related

How do I select some rows and then others in MySQL?

I have this query, it works but I'm not sure if it's the best approach and I don't get what I want.
I need to select the query contained in the "IN" clause first, then union with others. Entire row returned must be 40.
SELECT *
FROM (
SELECT * FROM tbl_x a WHERE id IN(11,20,30)
UNION ALL
SELECT * FROM tbl_x b WHERE exam_group='jpx' AND subject='chemistry'
) ab
GROUP BY id LIMIT 40
The next query should to return same data in simple way:
SELECT *
FROM tbl_x
WHERE
id IN (11,20,30)
OR (exam_group='jpx' AND subject='chemistry')
ORDER BY id IN (11,20,30) DESC, id
LIMIT 40;

How can I view list of values in a one column by MYSQL

How can I set a list of values by MYSQL in one column like the following query:
SELECT (100005355, 100001548, 100000672, 100001230, 100003382, 100009937, 100003778, 100002486, 100003301, 100005056, 100003435, 100003666, 100002761, 100008818, 100008341, 100003438, 100000581, 100009800, 100007165) AS ID;
result needs to be like this list
You can do it with UNION ALL:
SELECT 100005355 AS ID UNION ALL
SELECT 100001548 UNION ALL
SELECT 100000672 UNION ALL
SELECT .....
If you want to create a new table:
CREATE TABLE tablename AS
SELECT 100005355 AS ID UNION ALL
SELECT 100001548 UNION ALL
SELECT 100000672 UNION ALL
SELECT .....
In MySql 8.0+ you can use the VALUES statement with ROW constructor:
WITH cte(ID) AS (VALUES ROW(100005355), ROW(100001548), ROW(100000672))
SELECT * FROM cte
See the demo.
if you want just show the value you could try using group_concat
select group_concat(id) as my_col
from your_table
or use the saperator you prefer
select group_concat(id separator ', ') as my_col
from your_table
Did you tried this
select CONCAT_WS("", 100005355, 100001548, 100000672, 100001230, 100003382, 100009937, 100003778, 100002486, 100003301, 100005056, 100003435, 100003666, 100002761, 100008818, 100008341, 100003438, 100000581, 100009800, 100007165 ) AS ID;

Php mysql bad query when displays on a table

there's my code that select 5 databases and display in a table, but the where statement not work
The where is ignored by the query.
SELECT *
FROM events
UNION ALL
SELECT * FROM eventstwo
UNION ALL
SELECT * FROM eventsthree
UNION ALL
SELECT * FROM eventsfour
UNION ALL
SELECT * FROM eventsfive where atender='$obj'
I suspect that you want the WHERE criteria to apply to every subquery in the union. If you want that, you'll have to add a WHERE clause to each subquery. But, if you really do want to use a single WHERE clause, you can wrap your union query and then subquery it:
SELECT *
FROM
(
SELECT * FROM events
UNION ALL
SELECT * FROM eventstwo
UNION ALL
SELECT * FROM eventsthree
UNION ALL
SELECT * FROM eventsfour
UNION ALL
SELECT * FROM eventsfive
) t
WHERE atender = '$obj';
Side note: Please use prepared statements in your PHP code wherever possible.
SELECT * FROM
( SELECT *
FROM events
UNION ALL
SELECT * FROM eventstwo
UNION ALL
SELECT * FROM eventsthree
UNION ALL
SELECT * FROM eventsfour
UNION ALL
SELECT * FROM eventsfive)
AS derived
WHERE atender='$obj'

How do i select the most recent datetime from 2 tables

I've searched around but can't find the answer I'm looking for.
I have two tables, each has a date field called last_update_date. I want to search which of the two tables has the most recent date and only return that date.
I tried this query hoping it will order the two tables last_update_date field and return that result but the query does not work. Any help would be appreciated.
"Select last_update_date from Table1, Table2 order by last_update_date DESC Limit 1"
SELECT MAX(last_update_date)
FROM (
SELECT MAX(last_update_date) as last_update_date
FROM Table1
UNION ALL
SELECT MAX(last_update_date)
FROM Table2
) tMax
select * from
( select last_update_date from table1
UNION ALL
select last_update_date from table2
) order by last_update_date;
SELECT MAX(A.last_update_date)
(SELECT last_update_date FROM Table1
UNION ALL
SELECT last_update_date FROM Table2) A;

Filter by COUNT(*)?

Is it possible to group results and then filter by how many rows are in the group?
Something like this:
SELECT * FROM mytable WHERE COUNT(*) > 1 GROUP BY name
You want to use HAVING to filter on the aggregate function.
SELECT name, COUNT(*)
FROM mytable
GROUP BY name
HAVING COUNT(*) > 1
You need to use HAVING
SELECT * FROM mytable GROUP BY name HAVING COUNT(*) > 1
Although, SELECT * doesn't make much sense when you're grouping. I assume it's just for an example
You want a HAVING clause.
SELECT *
FROM mytable
GROUP BY name
HAVING COUNT(*) > 1
Use having in your query:
SELECT * FROM mytable GROUP BY name having COUNT(*) > 1