I have 2 different tables that each have a column called product_type. How can I get the DISTINCT values of product_type across both tables? Just to clarify, if both tables have a product_type of "diamond" I only want it returned once. Basically as if both tables where combined and I selected distinct product_type from it.
Thanks!!
Use a distinct with a subquery which contains a union
select distinct product_type from (
select product_type from table 1
union
select product_type from table 2
) t
Use distinct and union:
select distinct product_type from table1
union
select distinct product_type from table2
The union will remove duplicates when combining the results.
Note that the UNION clause returns unique values of the field, when you want it to return ALL the values you must use UNION ALL...
select product_type from table_a
union
product_type from table_b
Related
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)
I have two tables, training_taken_november and qtr1_copy. Each of them have a unique field. I want to join these two tables and count the unique values from both tables where their category = 'livestock', illustration below:
Tbl1 values might be: 1,2,3,4,5
Tbl2 values might be: 6,7,8,1,2
The count of distinct values should return: 8, that is, when we join the two tables they have 8 unique values in mentioned fields (name of fields that have unique values also differ).
Select count(*) from
(
Select id from ... Where category...
Union
Select id from ... Where cat ...
) x
Try this:
Select count(*) from
(select tazkira from Training_taken_november where category='Livestock'
union
select bene from qtr1_copy where cat='Livestock')x
I have four tables with same fields. Now I want to join these tables in such a way that I retrieve records only if there is a match between any two tables on a field(like name).
Thanks in advance.
This would return all the name values that appear in more than one table:
select
name
from
(select distinct
name
from table1
union all
select distinct
name
from table2
union all
select distinct
name
from table3
union all
select distinct
name
from table4) temp
group by name
having count(*) > 1;
Check out the interactive example.
I have two tables, both having column a device_id column that I want to count. For the purposes of demonstration, the schema looks like:
Table 1: 'id', 'save_val', 'device_id_major'
Table 2: 'id', 'save_val', 'location', 'device_id_team'
Table 1 could have many of the same 'device_id_major'.
I basically want to get the unique device_id's from both tables, then from that result set, get the count of unique device_id's (the same device_id can appear in both tables).
Is this possible in one query?
select distinct aa.device_id, count(*)
from(select distinct device_id from table1
union all
select distinct device_id from table2) as aa
group by device_id
order by device_id
Or something like... As I don't have the schema to hand, I can't fully validate it.
SELECT count(DISTINCT aa.id)
FROM (SELECT DISTINCT major_id AS id FROM `major`
UNION ALL
SELECT DISTINCT team_id AS id FROM `team`)
AS aa
This seems to do the trick.
You could use a query that takes the UNION of both tables, then SELECT the unique values.
I have two tables a and b which has a field name in it.
I need to list the data from these two tables. I thought of using union but in the result list data from the first table appears and then followed by the second.
what i want is to order by the field name so the result should be a mixed up of two tables in the order of name that is order by name.
select slug, name, 1 as mt
from tablea
union
select slug, name, 0 as mt
from tableb
order
by name;
The above is working well for me. will there be any complications in the result of this?
Suppose your query is
SELECT field1 FROM TABLE1 WHERE 1
UNION SELECT field1 FROM TABLE2 WHERE 1
u can make it a subquery like this
SELECT * FROM (SELECT field1 FROM TABLE1 WHERE 1
UNION SELECT field1 FROM TABLE2 WHERE 1) AS `result` ORDER BY `result`.`field1`
Or, you could use a Join query such as:
SELECT tablea.firstname, tablea.middlename, tablea.lastname, tableb.phone
FROM tablea, tableb
WHERE tablea.ID = tableb.ID
Then, you could sort the result however you like.