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)
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;
I would like to get a value returned (in this case a zero) from the query for types in the event that a count of Table.id is zero.
SELECT COUNT(Table1.id) AS AccountID, Table2.typeName AS Types
FROM Table1
LEFT JOIN Table3
ON Table1.productID = Table3.productID
LEFT JOIN Table2
ON Table3.categoryID = Table2.CategoryID
WHERE Table3.account_id = 51
GROUP BY Table2.typeName;
This is the result that I would like to get
This is the result that I am currently getting
Table1
id ProductID
1 1
1 1
2 2
2 1
1 1
id in this table 1 references categoryID in table2
---------------------------------------------------------
Table2
categoryID typeName
1 SUV
2 BMW
3 Toyota
4 Audi
5 Suzuki
---------------------------------------------------------
Table3
categoryID ProductID account_id
2 1 51
1 2 52
3 1 51
categoryID is table3 references categoryID in table2
----------------------------------------------------------
My expected result should look like this
Count Types
0 Toyota
0 Audi
0 Suzuki
3 SUV
2 BMW
I want a count of all the id(s) from table1 with its corresponding typeName. If the id is not present, I want the a 0 to be returned with the corresponding typeName.
Following query will work:
SELECT
COUNT(Table1.id) AS AccountID,
Table2.typeName AS Types
FROM Table2
LEFT JOIN Table3
ON Table3.categoryID = Table2.CategoryID
AND Table3.account_id = 51
LEFT JOIN Table1
ON Table1.id = Table3.CategoryID
GROUP BY Table2.typeName;
Click here for DEMO
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
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)
Can some one help me with a mysql query to show duplicates from each group in temporary table using mysql
sqlfiddle
http://sqlfiddle.com/#!2/d8c9c/17
Table
TOWN ID1
town1 1
town1 1
town1 4
town2 1
town2 5
town2 8
town3 1
town3 3
town3 3
Required result
TOWN ID1
town1 1
town1 1
town3 3
town3 3
I have tried SELECT * FROM Table1 group by TOWN,ID1
but this removes duplicates and also shows non-duplicate records
This is the query you're looking for
SELECT TOWN, ID1 FROM Table1 t WHERE ( SELECT COUNT(*) FROM Table1 WHERE TOWN = t.TOWN AND ID1 = t.ID1 ) > 1;
SELECT a.TOWN, a.ID1 FROM Table1 a JOIN
(SELECT TOWN, ID1 FROM Table1 GROUP BY TOWN, ID1 HAVING COUNT(*) > 1) b
ON a.TOWN = b.TOWN
AND a.ID1 = b.ID1