How to combine different querys with different conditions - mysql

I've three tables: a, b and c
a:(q,r,s), b:(q,r,t), c:(q,r,u,v)
I want to make a query like this:
select q,r from a where s="whatever", q="whatever"
select q,r from b where t="whatever", q="whatever"
select q,r from c where u="whatever", q="whatever"
I want the results in one query, order by r. Is that possible?

You can use the SQL UNION operator. It is used to combine the result sets of 2 or more SELECT statements. It removes duplicate rows between the various SELECT statements.
Each SELECT statement within the UNION must have the same number of fields in the result sets with similar data types.
Example:
select q,r from a where s="whatever", q="whatever"
UNION
select q,r from b where t="whatever", q="whatever"
UNION
select q,r from c where u="whatever", q="whatever"
ORDER BY 1 -- If you want to order by specific colum and columns'name are different in the two select
You can use UNION ALL instead of UNION. The difference between them is that UNION ALL does not remove duplicate rows.

Related

How to duplicate returning values from a query in MySQL

When I use this I Get the table two times but I want each value duplicated before the next value comes
SELECT *
FROM student
WHERE major LIKE 'C%'
UNION ALL
SELECT *
FROM student
WHERE major LIKE 'C%';
You can use a union all. A union will not work, because it will eliminate duplicates. Another way is a cross join:
select
*
from
students t1 cross join
(select 1 as n union all select 2) n
WHERE major LIKE 'C%'
order by id;

SQL query with repeated values in list

How to write a query correctly to get data without uniqueness?
I have list of ids, where ids are repeated.
Example: (1,1,1,2,3)
select *
from table
where id in (1,1,1,2,3);
returns only (1,2,3).
But I need to get with repeated entries.
Use a derived table and left join:
select t.*
from (select 1 as id union all select 1 union all select 1 union all select 2 union all select 3
) i left join
t
on t.id = i.id
The syntax for the derived table might vary depending on the database, but most support the above syntax.
That's not what WHERE statement is for, as it's only for filtering matching keys.
If you need to do that in this order, use sth like
select table.*
from (
select 1 as id
union select 1
union select 1
union select 2
union select 3
) myStaticKeys
join table using (id)

Mysql Union in different columns

I have the following query
SELECT *
FROM(
(SELECT
MAX(c.start_time) as start_1
FROM
c1 c)
UNION ALL
(SELECT
MAX(cc.created_at) as ccmax
FROM
cc1)
) as t
I'd like to have the result in a table with 2 columns start_1 and cmax instead of the single column I get with all the different results listed.
How should I do it? I ended up in a subselect believing this would have done the job.
For the data to be in two columns you would have to use a sub select.
SELECT
MAX(c1.start_time) as start_1, (SELECT MAX(cc1.created_at) FROM cc1) as ccmax
FROM c1

mysql union getting 2 column names from another table corresponding to 2 id columns

I have a table "task" which stores the details of a task that is created, I need to get the names of the creator ID and the ASSIGNEDTO ID in a single query for which i created a UNION however I get the following error in mysql
Both the select queries execute perfectly as individual queries
#1222 - The used SELECT statements have a different number of columns
SELECT a.task_id, a.task_title,a.task_created_by,a.task_creation_date,b.user_id, b.user_email FROM task a,users b WHERE a.task_created_by = b.user_id
UNION
SELECT a.task_assigned_to, b.user_email FROM task a,users b WHERE a.task_assigned_to = b.user_id
Only selects based on same columns can be subjects to UNION
There's a workaround that implies using "as" for any column that you might not have in the other table. Like this:
SELECT b, '' as c FROM table1
UNION ALL
SELECT b, c FROM table2
Based on this the "c" for table1 will always be empty, but the UNION will still work.
Use this logic to UNION tables that have different columns

order multiple tables according to one column

Is it possible, using SQL to pull data from different tables, then sort the data according to one column that is in all tables. eg, I have 3 tables. Base, Selects, Sub. They all 3 have a position column,
Select base_layers.position, selects.position, subbases.position
from base_layers,selects,subbases
Order By (alls)position;
That is exactly what I actually want to do... But have a feeling it is not possible.
Use a union:
(I'm assuming you don't want to cross join all 3 tables)
select Position
from
(
select base_layers.position AS Position
from base_layers
union
select selects.position
from selects
union
select subbases.position
from subbases
) x
order by Position ASC;
Your syntax was incorrect. Try this version.
SELECT * FROM
(
SELECT base_layers.position
FROM base_layers
UNION ALL
SELECT selects.position
FROM selects
UNION ALL
SELECT subbases.position
FROM subbases
) tbl ORDER BY tbl.position;