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'
Related
with hotels as (
select * from hotel_revenue_historical_full
union
SELECT * FROM projects.`db.2019`
union
SELECT * FROM projects.`2020`);
select * from hotels;
select stays_in_weekend_nights + stays_in_week_nights from hotels;
select arrival_date_year,hotel, sum ((stays_in_weekend_nights + stays_in_week_nights) * adr) as revenue from hotels group by arrival_year, hotel;
-- save the data into temptable
CREATE TEMPORARY TABLE hotels
select * from hotel_revenue_historical_full
union
SELECT * FROM projects.`db.2019`
union
SELECT * FROM projects.`2020`;
-- query needed information
select *, stays_in_weekend_nights + stays_in_week_nights from hotels;
select arrival_date_year,hotel, sum ((stays_in_weekend_nights + stays_in_week_nights) * adr) as revenue from hotels group by arrival_year, hotel;
-- drop temptable
DROP TEMPORARY TABLE hotels;
If the table hotel is not huge then you may add ENGINE = Memory to its definition which will improve the process.
Also I recommend you to use not UNION but UNION ALL.
my Query is
select * from `database_1`.`table_name`
union all
select * from `database_2`.`table_name`;
this Query Run In 0.0074 - 0.0170 seconds
But when create View with this Query
create View `view_name` as
select * from `database_1`.`table_name`
union all
select * from `database_2`.`table_name`;
select * from `view_name`;
this Query Run In 0.0800 - 0.1200 seconds
Notice: this view is on tow databases.
Thanks
I have pm_message tables with 1~9, and I want to create a view to simplified the process of MySQL query.
What I have is
CREATE VIEW `pm_messages` AS
SELECT * FROM
`pm_messages_0`,
`pm_messages_1`,
`pm_messages_2`,
`pm_messages_3`,
`pm_messages_4`,
`pm_messages_5`,
`pm_messages_6`,
`pm_messages_7`,
`pm_messages_8`,
`pm_messages_9`;
I got error with douplicate column. There is no record is duplicate, I want to merge all of them in view, what should I do?
You have coded a colossal cross join. Depending on the number of rows, it probably wouldn't return before the universe suffers entropy heat death.
I'm almost certain you want a union:
CREATE VIEW `pm_messages` AS
SELECT * FROM `pm_messages_0` union all
SELECT * FROM `pm_messages_1` union all
SELECT * FROM `pm_messages_2` union all
SELECT * FROM `pm_messages_3` union all
SELECT * FROM `pm_messages_4` union all
SELECT * FROM `pm_messages_5` union all
SELECT * FROM `pm_messages_6` union all
SELECT * FROM `pm_messages_7` union all
SELECT * FROM `pm_messages_8` union all
SELECT * FROM `pm_messages_9`;
This will work if all tables have the same number and type of columns. If not, you'll have to explicitly select columns such that each select returns the same number and type of columns.
I have the following views
M_VWPROC_sub1,M_VWPROC_sub2,M_VWPROC_sub3,M_VWPROC_sub4,M_VWPROC_sub5,
M_VWPROC_sub6,M_VWPROC_sub7,M_VWPROC_sub8,M_VWPROC_sub9
I tried to create a view using the following sql.but I get the following error
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use
near 'M_VWPROC_sub2 union all M_VWPROC_sub3 union all M_VWPROC_sub4
union all M_VWPROC' at line 2
Query:
CREATE OR REPLACE VIEW M_VWPROC (UNIT, PS_DATE, LOCALIN, STOCKIN, STOCKOUT) AS
select inner_sub_table.UNIT,
inner_sub_table.PS_DATE,
sum(inner_sub_table.LOCALIN)LOCALIN,
sum(inner_sub_table.pur) STOCKIN,
sum(inner_sub_table.sale) STOCKOUT
from M_VWPROC_sub1
union all M_VWPROC_sub2
union all M_VWPROC_sub3
union all M_VWPROC_sub4
union all M_VWPROC_sub5
union all M_VWPROC_sub6
union all M_VWPROC_sub7
union all M_VWPROC_sub8
union all M_VWPROC_sub9)
inner_sub_table
group by inner_sub_table.UNIT, inner_sub_table.PS_DATE;
Any help. Thanks in advance.
The correct syntax for union all requires a subquery . . . that is a select clause. But, you cannot do what you want. You need another layer of subqueries. Something like:
create view v_YetAnotherView as
select * from M_VWPROC_sub1
union all select * from M_VWPROC_sub2
union all select * from M_VWPROC_sub3
union all select * from M_VWPROC_sub4
union all select * from M_VWPROC_sub5
union all select * from M_VWPROC_sub6
union all select * from M_VWPROC_sub7
union all select * from M_VWPROC_sub8
union all select * from M_VWPROC_sub9;
And then:
CREATE OR REPLACE VIEW M_VWPROC (UNIT, PS_DATE, LOCALIN, STOCKIN, STOCKOUT) AS
select inner_sub_table.UNIT,
inner_sub_table.PS_DATE,
sum(yav.LOCALIN)LOCALIN,
sum(yav.pur) STOCKIN,
sum(yav.sale) STOCKOUT
from YetAnotherView yav
group by yav.UNIT, yav.PS_DATE;
Do note that this ridiculous limitation on views (that the from clause cannot contain subqueries) is limited to 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.