MySql Join tables - mysql

I have two tables like this.
Table 1 Fields:
CId , Name
Table 2 Fields:
CId , food
I want to get no. of food against each CId with the query "select * from table1"

Something like that should work:
SELECT
t1.* , count(t2.food) as foods
FROM
t1 LEFT JOIN t2 on (t1.Cid = t2.Cid)
GROUP BY
t2.Cid

SELECT
food
FROM
Table2 t2
JOIN Table1 t1 ON (t2.Cld = t1.Cld)

select a.name, b.food from table1 a, table1 b where a.cld = b.cld;

Related

Select distinct pairs in SQL

For this table each column has distinct rows i.e count(Name) = count(Spouse_Name) = count(*)
Want to write a SQL query where pairs (Name and Spouse_Name) are distinct i.e Alex Sandra and Sandra Alex are same.
The output should be following:
Note: Self Inner Join if possible.
Untested:
select distinct t1.name, t1.spouse_name
from table t1
inner join table t2
on concat(t1.name, t1.spouse_name) = concat(t2.spouse_name, t2.name)
I couldn't find the table name in your post so I used the name table.
Assuming that name and spouse_name will never be equal in the same row you can use NOT EXISTS like this:
select t.*
from tablename t
where t.name < t.spouse_name
or not exists (
select 1 from tablename
where name = t.spouse_name and spouse_name = t.name
)
This code will work in any database since it uses standard SQL.
For MySql and this sample data you can do this self join:
select distinct
least(t1.name, t1.spouse_name) name,
greatest(t1.name, t1.spouse_name) spouse_name
from tablename t1 inner join tablename t2
on t1.name = t2.spouse_name and t2.name = t1.spouse_name
Although the same results can be obtained without a join:
select distinct
least(t1.name, t1.spouse_name) name,
greatest(t1.name, t1.spouse_name) spouse_name
from tablename t1
And another query with a self join:
select t1.name, t1.spouse_name
from tablename t1 inner join tablename t2
on t2.spouse_name = t1.name and t2.name = t1.spouse_name
where t1.name < t1.spouse_name

SQL JOIN and count without counting an ID twice

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

Can a table get other table column data

I need to create a select statement where the statement need to retrieve data from other table column data
eg.
Table1 Table2
id id2
age age2
Select id, age from table 1 where id= id2
Is that possible.
You can use INNER JOIN
SELECT
T1.id,
T1.age
FROM Table1 AS T1
INNER JOIN Table2 AS T2
ON T1.id = T2.id2
DEMO using INNER JOIN
You can use EXISTS
SELECT
T1.id,
T1.age
FROM Table1 AS T1
WHERE EXISTS(
SELECT 1
FROM Table2 AS T2
WHERE T2.id2 = T1.id
);
You can use IN
SELECT
T1.id,
T1.age
FROM Table1 AS T1
WHERE T1.id IN (SELECT T2.id2 FROM Table2 AS T2)
Note:
In the working demo the output consists of two rows. There are two entries in tabel1 and three entries in table2. But there are only two matching entries found between these two tables. That's why output consists of only two rows.
Yes you can. It is called a JOIN and there are several types of JOINs. I suggest you read up on them on SQL JOINs.
SELECT id ,age
FROM TABLE 1
WHERE id IN (SELECT id2 FROM TABLE2);
OR
SELECT id ,age
FROM TABLE1 , TABLE2
WHERE id = id2 ;
OR
SELECT id ,age
FROM TABLE 1 , (SELECT id2 FROM TABLE2) TBL2
WHERE id = TBL2.id2 ;

Select count with join in SQL query

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;

use a mysql query result to do another query

I have 2 table table1 contains an userid and a postid. Table2 contains a userid and a username. I want to return all the userids with a certain postid and then use those userids to query table2 and get the usernames.
Is there a way to go about this? I have tried join statement and it doesn't seem to work
Use the IN function:
select username, userid from `table2` where userid in (select userid from `table` where postid = <condition>)
SELECT A.postid, A.userid, B.username FROM
tableA AS A JOIN tableB AS B ON A.userid=B.userid
select post id, user id from tableA username from tableB. Join these 2 different table columns when tableA's userid is equal to tableB's userid.
select t1.postid, t1.userid, t2.username
from table1 t2
left join table1 t2 on t1.userid = t2.userid
where t1.postid = <your postid>
Join the tables on the common columns
select t1.postid, t1.userid, t2.username
from table1 t1
join table2 t2 on t1.userid = t2.userid
where t1.postid = 2