How can I check how many products added by a,b,c,d respectively by using a query?
table1
admin_id admin_name
3 a
4 b
5 c
6 d
table2
admin_id products
3 pDeal
3 pSeal
4 pAeal
5 pZeal
6 pXeal
3 pHeal
6 pPeal
You need a simple JOIN and a COUNT query:
SELECT table1.admin_name, COUNT(*) as cnt
FROM
table1 INNER JOIN table2
ON table1.admin_id = table2.admin_id
GROUP BY
table1.admin_name
Try this...
SELECT a.admin_name, COUNT(b.products) as 'CountOfProducts'
FROM table1 a INNER JOIN table2 b ON a.admin_id = b.admin_id
GROUP BY a.admin_name
Use this
SELECT adm.admin_name,COUNT(pdr.products) as ProductCnt
FROM table1 AS adm
JOIN table2 AS pdr
ON adm.admin_id = pdr.admin_id
GROUP BY adm.admin_id;
SELECT t1.admin_name, COUNT(t2.products)
FROM table1 AS t1 LEFT JOIN table2 AS t2 ON t1.admin_id = t2.admin_id
WHERE 1
GROUP BY t2.admin_id
The LEFT JOIN will ensure the cases when table2 doesn't have any record for some admin of table 1.
You can use an inner-select like this:
SELECT
table1.admin_name,
(SELECT COUNT(*)
FROM table2
WHERE table1.admin_id = table2.admin_id) As cnt
FROM
table1;
Related
Thanks for assisting with the previous query (SQL Query that selects a column in table 1 and uses that to select sum in table 2) of SUM from 2 tables, I now have a additional Condition for 1 of the tables. I would like to add WHERE Group1 = 1 AND IN/OUT = 'OUT'
I have 3 tables,
Names ,Groups
Names ,Payments
Names ,Payments and IN/OUT
I want to only SUM the OUT Payments in Table 3, I am getting total payments only So FAR is have:
SELECT t1.name1, SUM(t2.sale2),SUM(t3.sale3)
FROM table1 t1 JOIN table2 t2 ON t1.name1 = t2.name2
JOIN table3 t3 ON t1.name1 = t3.name3
WHERE group1 = 1
GROUP BY t1.name1
i would also like to add a zero if there is no data to sum instead of removing the whole record, Currently if a name has no payments in Table 3 but has payments in table 2 it deletes the record.
Please check the query below =>
To Get OutPayment group by Name
SELECT t1.Names,SUM(t3.Payments) As OutPayment
FROM TABLE3 as t3
INNER JOIN TABLE1 as t1 ON t1.Names = t3.Names
INNER JOIN TABLE2 as t2 ON t1.Names = t2.Names
WHERE t1.GroupID = 1 AND t3.INOROUT=2 --INOROUT =2 is OUT and 1 is IN
GROUP BY t1.Names;
To Get TotalOutPayment
SELECT SUM(t3.Payments) As TotalOutPayment
FROM TABLE3 as t3
INNER JOIN TABLE1 as t1 ON t1.Names = t3.Names
INNER JOIN TABLE2 as t2 ON t1.Names = t2.Names
WHERE t1.GroupID = 1 AND t3.INOROUT=2; --INOROUT =2 is OUT and 1 is IN
Note: Code is in DBFiddle too Check the Demo Query Link
I am trying to join a table and get a count but I cannot count an ID twice in the table for the count.
Table 1:
ID animal
-- ------
1 dog
2 dog
3 cat
4 cat
5 dog
Table 2:
ID
--
2
2
3
5
5
I need to get a count of how many of each type of animal are in table 2. I can get it to join and change the ID to the type of animal and then get a count of each.
The issue is that each ID can only get counted once. So the expected output would be.
dog:2
cat:1
Where my output is
dog:4
cat:1
Try like below
select t1.animal, count( distinct t2.ID)
from table1 t1 join table2 t2 on t1.ID=t2.ID
group by t1.animal
You can try below using count distinct id
select b.animal,count(distinct a.id) from table2 a
inner join table1 b on a.id=b.id
group by b.animal
Try this:
SELECT t1.animal AS "Animal", COUNT(DISTINCT t1.ID) AS "No. of Animals"
FROM TABLE2 t2, TABLE1 t1
WHERE t2.ID = t1.ID
GROUP BY t1.animal
You can Try Nested Selects here.
SELECT
t.animal,
COUNT(t.ID) AS Count
FROM
(SELECT DISTINCT a.animal, b.ID FROM table1 a INNER JOIN table2 b ON a.ID = b.ID)t
GROUP BY t.animal
this is tested image
I have three tables with following data
Table 3 :
Table1_id Table2_id
1 1
1 2
1 3
2 1
2 3
3 2
Table 2 :
Table2_id Name
1 A
2 B
3 C
Table 1 :
Table1_id Name
1 P
2 Q
3 R
I have a problem where I need to return all table1_id's which have an entry for all Table2_ids's in Table 3.
ie. I want my output to be
Table1_id
1
I found a solution using count().
But is there a way to use all() or exists() to solve the query?
Using NOT IN with excluding LEFT JOIN in a subselect with a CROSS JOIN
select *
from table1
where Table1_id not in (
select t1.Table1_id
from table1 t1
cross join table2 t2
left join table3 t3 using (Table1_id, Table2_id)
where t3.Table1_id is null
)
VS using COUNT()
select table1_id
from table3
group by table1_id
having count(1) = (select count(1) from table2)
Explanation:
The CROSS JOIN
select t1.Table1_id
from table1 t1
cross join table2 t2
represents how table3 would look like, if every item from table1 would be related to every item from table2.
A (natural) left join with table3 will show us which relations really exists. Filtering by where t3.Table1_id is null (excluding LEFT JOIN) we get the missing relations. Using that result for the NOT IN clause, we get only table1 items that have no missing relation with table2.
You can use the following query:
SELECT DISTINCT t1.*
FROM Table2 AS t2
CROSS JOIN Table1 AS t1
WHERE NOT EXISTS (SELECT 1
FROM Table3 AS t3
WHERE t1.Table1_id = t3.Table1_id AND
t2.Table2_id = t3.Table2_id)
to get Table1 records not having a complete set of entries from Table2 in Table3. Then use NOT IN to get the expected result.
Here is a solution using EXISTS and INNER JOIN.
SELECT DISTINCT t3_out.Table1_id FROM Table3 t3_out
WHERE EXISTS( SELECT 1
FROM Table2 t2 INNER JOIN Table3 t3 ON t2.Table2_id = t3.Table2_id
WHERE t3.Table1_id = t3_out.Table1_id
HAVING COUNT(DISTINCT t2.Table2_id) = 3 )
good day, i have a problem in querying in 2 mysql table.
table 1
c_id c_name
**-----------------**
1 blah,blah
2 hey
table 2
m_id c_id m_name
-----------------------
1 1 some_name
2 1 some-name
3 1 some/name
4 1 some.name
5 2 name.some
6 2 name-some
i wanted the display to be like this:
c_id c_name m_name
-----------------------------
1 blah,blah some_name
some-name
some/name
some.name
2 hey name.some
name-some
Select a.c_id, a.c_name,
group_concat(b.m_name SEPARATOR '<br/>') as m_name from table1 a
left join table2 b on a.c_id = b.c_id
group by a.c_id;
Fiddle
Use m_name bind to a label. It will come with line breaks
This query will output exactly what you want :
select case when first.m_id is null then ' ' else t1.c_id end c_id,
case when first.m_id is null then ' ' else t1.c_name end c_name,
m_name
from Table1 t1
inner join Table2 t2 on t1.c_id = t2.c_id
left outer join (
select m_id,t1.c_id, t1.c_name, min(m_name)
from Table1 t1
inner join Table2 t2 on t1.c_id = t2.c_id
group by t1.c_id, t1.c_name) first on t2.m_id = first.m_id
order by t1.c_id, t2.m_id
See SQLFIDDLE : http://www.sqlfiddle.com/#!2/4bab3/16/0
You can use join to get the results of both tables together:
select c_id, c_name, m_name from table1 as x left outer join table2 as y on x.c_id = y.c_id;
Select a.c_id, a.c_name, b.m_name from table1 a left join table2 b on a.c_id = b.c_id;
table1
cid
itemdesc
itemprice
table2
cid
imagename
status
My 1st table is has unique cid (no duplicate) I want it to LEFT JOIN TO table2 but it has multiple rows per cid
cid imagename status
1 image1-of-cid1 test1
1 image2-of-cid1 test2
2 image1-of-cid2 test3
2 image2-of-cid2 test4
2 image3-of-cid2 test5
But I only want the Query to return the the 1st row only of the each record fom table 1
Thanks
I agree with John Woo's answer above. You need a subquery of some kind to actually retrieve the first row of table 2. Something like:
SELECT
t1.[id],
t2.*
FROM table1 AS t1
LEFT JOIN table2 AS t2
ON t2.cid = (SELECT TOP 1 cid FROM table2 WHERE cid = t1.cid)
you need to create an extra subquery that gets one imagename per cid. try this,
SELECT a.*, b.*
FROM table1 a
LEFT JOIN
(
SELECT cid, MIN(imagename) minImage
FROM table2
GROUP BY cid
) c ON a.cid = c.cid
LEFT JOIN table2 b
ON c.cid = b.cid AND
b.imageName = c.minImage
SQLFiddle Demo
Select
distinct a.cid,a.itemdesc,b.imagename,a.itemprice,b.status
from table1 a,
table2 b
where a.cid=b.cid
Try this:
SELECT a.cid, a.itemdesc, a.itemprice, b.imagename, b.status
FROM table1 a
LEFT OUTER JOIN table2 AS b ON a.cid = b.cid
GROUP BY a.cid, a.itemdesc, a.itemprice;