Combine queries in MySQL - mysql

I have two table in MySQL that looks as follows:
ID Name Information
1 A fsdf
2 B ada
3 A dsafd
4 A retret
5 C asdfsa
6 B xzc
and,
P_ID Name Loc_X Loc_Y
1 A 2 3
2 B 3 4
3 C 4 5
I would like to run a query in MySQL that return the result as follows:
NAME COUNT Loc_X Loc_Y
A 3 2 3
B 2 3 4
C 1 4 5
Currently, I am able to execute the following query:
SELECT Name,COUNT(*) as count FROM Table_A GROUP BY Name ORDER BY count DESC;
to get the following result:
NAME COUNT
A 3
B 2
C 1
I know that probably I can use this result to extract only the "Name" and fire another query to get the Loc_X and Loc_Y using PHP, but I was wondering whether there is a moe efficient way of doing it using DML. Is there a way to nest the queries?

Try something like this :
SELECT Table_A.Name,COUNT(*) as count, Table_B.Loc_X, Table_B.Loc_Y
FROM Table_A
INNER JOIN Table_B ON Table_A.name = Table_B.name
GROUP BY Table_A.Name, Table_B.Loc_X, Table_B.Loc_Y
ORDER BY count DESC;

I think you just need to join the tables on the common field here.
Try:
SELECT a.Name, count(a.id) AS Count b.Loc_X, b.Loc_Y
FROM Table_A a
INNER JOIN Table_B b ON a.Name = b.Name
GROUP BY a.Name

Related

Joining tables and getting a count of specific record

I am having two tables say table_a and table_b with following structure.
table_a : ID(primary key), value_one
table_b: ID, value_two
Note that id in table_b is not primary and contains multiple records to same id.
now i want a third table which displays a record for every id in table_a with columns being
id column_count
the column _count will display number of records (count) in table_b with value_two = 'c'. and i want to iterate this to all records of table_a.
For example lets say our table_a is like this:
id value_one
1 20
2 40
3 50
table_b
id value_two
1 10
1 20
1 10
2 40
2 10
3 40
3 10
I want records with value_two = 10 so my new table would look like
id count
1 2
2 1
3 1
Since id 1 has two records with value_two = 10 and id 2 and id 3 have one record each with value_two = 10
One way of doing this uses a correlated subquery:
select a.id,
(select count(*) from table_b b where b.id = a.id and b.value_two = 10) as cnt_10
from table_a a;
Another method uses a left join:
select a.id, count(b.id)
from table_a a left join
table_b b
on b.id = a.id and b.value_two = 10
group by a.id;
In your example data, this works:
select b.id, count(*)
from table_b b
where b.value_two = 10
group by b.id;
This is equivalent under the following circumstances:
All ids in a are in b.
All ids have at least one value of 10.
If these two conditions are true, then use this simpler query.
You can do conditional aggregation :
select id, sum(value_two = 10) as count
from table_b tb
group by id;
If you want matching ids then add INNER JOIN. This will show 0 count whereas value_two = 10 not found. You can add where clause to find only value_two = 10 count.

left join two tables where my variable is less than 5

Im creating a query that select two tables and create a total variable by count a field in one table.
Example:
Table A:
ID | email
1 | test#test
2 | test2#test
3 | test3#test
Table B
ID | email_id | username_id
1 | 1 | 11
2 | 1 | 22
3 | 2 | 33
My query:
select a.id, a.email, count(c.id) as total
from tableA a
left join tableC c on c.email_id = a.id AND total <= 5
group by a.email LIMIT 1
Output:
Unknown column 'total' in 'on clause
I need to select the first "a.id" that has total <= 5. How can I do it?
Logically Select is processed after the Where clause so you cannot use Alias name in same Where clause.
Use HAVING clause
select a.id, a.email, count(c.id) as total
from tableA a
left join tableC c on c.email_id = a.id
group by a.email
Having count(c.id) <= 5
LIMIT 1
I think Mysql allows you do this as well
Having total <= 5
Try HAVING Count(c.id) <= 5
Just to make this a bit clearer, since the correct answer has already been provided - You don't have to use the HAVING clause, and the HAVING clause is not always the solution for this problem.
The HAVING clause is usually used to place filters on aggregated columns (sum,count,max,min etc..) , but when you have a calculated column (colA + colB as calc_column for example) , then another approach , which should work here as well is to wrap the query with another select, and then the new column will be available on the WHERE :
SELECT *
FROM (The query here ) s
WHERE s.total <= 5

Filter count table query

i have 2 tables as following.
User
id name
---------------
1 john
2 raju
3 manu
4 raghu
friendtable
id userid recvId
------------------------
1 1 2
2 1 3
3 2 3
4 3 4
Is it possible to filter users by their friends count from these tables.Please help me.
For eg :- range >=3 will result : john,manu
range >3 and range <2 will result : raju
range <2 result : raghu
Do a UNION ALL to get all id's from friendstable in one column. Join users table with that result.
Do a GROUP BY, adjust HAVING to decide what to return, e.g. at least 3 times etc.
select u.name
from users
join (select userid as id from friendtable
union all
select recvId as id from friendtable) f
on u.id = f.id
group by u.name
having count(*) >= 3
SELECT name FROM user a,friendtable b WHERE a.id=b.id AND b.recvid>=3
SELECT name FROM user a,friendtable b WHERE a.id=b.id and b.recvid>3 AND b.recid<2
SELECT name FROM user a,friendtable b WHERE a.id=b.id AND b.recid<2

How to use limit and group by together in mysql

With these sample tables
table_a table_b
column_1 column_1 column_2
1 1 A
2 1 B
3 2 C
4 3 D
5 4 E
the query below
SELECT table_a.column1,table_b.column2
FROM table_a
INNER JOIN table_b ON table_a.column1 = table_b.column_1
GROUP BY table_a.column1 LIMIT 3
gives only 2 results (limit is 3) since the value 1 is duplicating in table_b. How can i get 3 results with unique table_a.column1 values. In general how can i use group by and limit together with group by having no impact on the limit
After minor adjustments with columns names (changing column1 into column_1 and column2 into column_2) your query gives me exactly 3 rows of results.
1 A
2 C
3 D

MySQL query matching multiple fields?

I'm confused on how to write the following query in MySQL: Given one collaborator, I'd like to get all the collaborators that participated on each item that this collaborator worked on.
Here's my collaborators table:
id collaborator_id item_id
1 1 1
2 2 1
3 3 1
4 4 2
5 1 2
6 2 3
for collaborator_id=1, the query would return:
collaborator_id item_id
1 1
2 1
3 1
1 2
4 2
So collaborator_id=1 worked with collaborator_ids=2,3 on item_id=1 and worked by themselves on item_id=2.
This seems super simple but I'm having a brain freeze on how to get these results. Thoughts?
This query joins on the item_id and gets you all the unique collaborators who worked with a given collaborator on shared items, other than the collaborator himself:
SELECT distinct b.collaborator_id, b.item_id
FROM collab_table a
JOIN collab_table b
on a.item_id = b.item_id
WHERE a.collaborator_id = 1
and b.collaborator_id != 1
you can use subquery
SELECT *
FROM collaborator
WHERE item_ID IN
(
SELECT item_ID
FROM collaborator
WHERE collaborator_id = 1
)
ORDER BY item_ID, Collaborator_ID
or by using JOIN
SELECT a.*
FROM COLLABORATOR a
JOIN COLLABORATOR b
ON a.ITEM_ID = b.ITEM_ID
WHERE b.COLLABORATOR_ID = 1
ORDER BY item_ID, Collaborator_ID;
SQLFiddle Demo
What you need to do is join the table back to itself, using your filter on one of the aliases, and pulling the result set from the other:
select a.COLLABORATOR_ID, a.ITEM_ID
from COLLABORATOR as a
inner join COLLABORATOR as b on
a.ITEM_ID = b.ITEM_ID
where b.COLLABORATOR_ID = 1