I have two tables as follows:
table1:
user_id ancestry
--------- ----------
1 England
1 Ireland
2 France
3 Germany
3 Poland
4 England
4 France
4 Germany
5 France
5 Germany
table2:
country
---------
England
Germany
France
I need to write a sql query that will pick out all those User ID who has ancestry every country in the table2.How Can I achive that.
DISTINCT is need in case one user_id can have multiple ancestry from same country
SELECT user_id
FROM Table1
GROUP BY user_id
HAVING COUNT(DISTINCT ancestry) = (SELECT COUNT(*) FROM Table2)
EDIT:
I realize table1 can have different countries than table2 like Ireland.
So you probably need use JOIN to see how many match exists
SELECT T1.user_id
FROM Table1 T1
JOIN Table2 T2
ON T1.ancestry = T2.country
GROUP BY T1.user_id
HAVING COUNT(DISTINCT T1.ancestry) = (SELECT COUNT(*) FROM Table2)
Related
I'm trying to count multiple columns as one column. For example:
Table 1 (Books):
ID
BookName
Genre
SubGenre
1
Name1
1
3
2
Name2
2
1
3
Name3
4
2
Table 2 (Genre):
ID
Genre
1
Horror
2
Drama
3
Romance
4
Sci-Fi
I want to be able to count the genre and subgenre as one to create a table of:
Result:
Genre
Count
Horror
2
Drama
2
Romance
1
Sci-Fi
1
Any help would be really appreciated. Thanks
Try below query-
SELECT t.Genre, Sum(t.cg) AS Count
FROM (
SELECT t2.Genre, Count(t1.Genre) AS cg
FROM Table2 as t2 LEFT JOIN Table1 as t1 ON t2.ID = t1.Genre
GROUP BY t2.Genre
UNION ALL
SELECT t2.Genre, Count(t1.SubGenre) AS cg
FROM Table2 as t2 LEFT JOIN Table1 as t1 ON t2.ID = t1.SubGenre
GROUP BY t2.Genre
) as t GROUP BY t.Genre;
lets say i have 2 tables:
table 1:
PersonID PersonName
1 Micheal
2 Edward
3. Nord
4. Stephanie
Table 2
PurchaseID PurchaseItem. PersonID
1 Rack 1
2 Desk 1
3. Lamp 2
4. Table 3
with standard join, query result can return
1 Micheal Rack
2. Micheal Desk
3 Edward Lamp
4 Nord Table
but i need the result to be shown as:
1 Micheal Rack, Desk
2 Edward Lamp
3 Nord Table
You could group by the PersonName and use group_concat to concatenate the PurchaseItems:
SELECT t1.PersonId, PersonName, GROUP_CONCAT(PurchaseItem, ', ')
FROM t1
JOIN t2 ON t1.PersonId = t2.PersonId
GROUP BY t1.PersonId, PersonName
select t1.personid, t1.personname,
group_concat(t2.PurchaseItem)
from t1
join t2 on t1.personid = t2.personid
group by t1.personid, t1.personname
You can try something like this:
Select t1.personid, t1.personname, group_concat(t2.purchase_item)
from table1 t1 join table2 t2
on t1.personId = t2.personId
group by personID;
Table 1 :
ID City State
1 NewYork NY
2 Oklahama OK
3 california CA
4 new jersey NJ
5 Las Vegas LA
Table 2 :
ID City
1 NewYork
2 NewYork
3 NewYork
4 Oklahama
5 Oklahama
NewYork is 3 times and Oklahama is 2 times in table 2.
So I want to fetch City List from Table 1 which Cities are used less then 5 times in Table 2
So what will exact query in Mysql?
I am using below code :
select *
from Table1
where Table1.city in
(select Table2 .city,count(*)
from Table2
having count(*) < 5
group by Table2.city )
You can use a in clause with a subselect
select * from table1
where city in (select city from table2 having count(*) < 5 group by city)
In your code you have a space between the Table2 and .city
select *
from Table1 where Table1.city in (select
Table2 .city,count(*) from Table2 having count(*) < 5 group by Table2.city )
must be
select *
from Table1
where Table1.city in (select Table2.city
from Table2
group by Table2.city
having count(*) < 5 )
no space between tablename and column ..
you can seee http://sqlfiddle.com/#!9/556cb6/10
How about
select Table1.city
from Table1 inner join Table2 on Table1.city = Table2.city
group by Table1.city
having count(Table2.city) < 5
SQLFiddle
My tables structure is :
Student:
ID Name From
1 student A England
2 student B China
3 student C USA
Subject:
ID id_student Subject
1 1 Maths
2 1 Physics
3 2 English
4 3 Physics
5 4 History
I want to get all data in main table (A) and all rows in have id_A in child table (B) to show in grid table like this :
ID Student Subject
1 student_A Maths, Physics
2 student_B English
3 student_C Physics, History
I wonder how to select data ?
You can use GROUP_CONCAT:
SELECT t1.ID, t1.Name, GROUP_CONCAT(t2.Subject)
FROM Student AS t1
LEFT JOIN Subject AS t2 ON t1.ID = t2.id_student
GROUP BY t1.ID, t1.Name
I have these two tables:
Table 1:
ID ITEM
--------------------
1 AB
1 22S
1 AB
2 F45
2 BB
3 1
3 1
3 AA
3 F45
3 F67
3 A
......
Table 2:
ITEM COUNTRY
---------------
0 usa
1 italy
2 mexico
3 mexico
A greece
AA malta
AB usa
AC pakistan
B uk
BB france
BA france
BB russia
F45 uk
And I use the following query to get the Items that are like "A%" for each ID:
SELECT GROUP_CONCAT(ITEM)
FROM (SELECT Table1.ID, Table1.ITEM, Table1.date
FROM Table2 Table2 INNER JOIN Table1 Table1
ON (Table2.ITEM = Table1.ITEM) WHERE table2.ITEM like "A%"
ORDER BY Table1.id, Table1.date, Table2.ITEM) as temp
GROUP BY ID
ORDER BY ID
I want to find a way to edit my query so I can get the above only for the IDs that happen to have item "F45" in them (but I don't want to use this item, just select the IDs that have it)..
Any help will be appreciated
I guess
SELECT GROUP_CONCAT(ITEM) AS group_item
FROM (SELECT Table1.ID, Table1.ITEM, Table1.date
FROM Table2 Table2 INNER JOIN Table1 Table1
ON (Table2.ITEM = Table1.ITEM) WHERE table2.ITEM like "A%"
ORDER BY Table1.id, Table1.date, Table2.ITEM) as temp
GROUP BY ID
HAVING group_item LIKE '%F45%'
ORDER BY ID
Or adjust the subquery to select only needed rows (here I'm not sure what are you trying to achieve)