How to get ONLY common rows of 2 tables on MySQL? - mysql

Probably is something simple, but, let's say I have these:
Table User (id_user, name)
Table A (id_a, name, type, #id_user)
And then I have another one that have only its own id and the other foreign keys
Table B (id_b, #id_user1, #id_user2, #id_a, #id_Something)
So, I need a query that returns ONLY the rows of table A and table B with what they have in common. I've tried INNER JOIN but it returns all rows of Table A where the id_user from there is equal to the id_user from table B. Like, if I have these:
Table User:
id_user name
1 Hey
Table A:
id_a name type id_user
1 a car 1
2 b cat 1
Table B:
id_b id_user id_user2 id_a id_Something
1 1 Doesn't matter 1 Doesn't matter
I need to return only the common row between Table A and Table B (that'll be something like:
id_a name type id_user id_b id_user2
1 a car 1 1
I've tried INNER JOIN but it returns to me everything when the id_user from A = id_user from B. I used this syntax:
SELECT *
FROM B
INNER JOIN A ON A.id_user = B.id_user;
Hope I've made myself clear, thank you a lot.

Is what you're going after: "Show me all the rows in A and B which share the same id_user"
SELECT User.id_user, User.name, a.id_a, b.id_b
FROM User
INNER JOIN A ON a.id_user = User.id_user
INNER JOIN B on b.id_user = User.id_user

Related

I'm trying to match different tables without a primary key

before posting this, I searched for an answer, but couldn't find the exact solution to my issue.
First of all, I am only assuming that I need INNER JOIN to solve it. FULL OUTER JOIN might also do the trick.
I have 3 different tables in one database.
TABLE 1
userId
roleID
1
1
2
2
TABLE 2
userId
userName
1
A
2
B
TABLE 3
roleId
roleName
1
X
2
Y
My goal is to write a query, which matches the userName from Table 2 with the roleName of Table 3 in the output. It should look like this:
Output
UserName
RoleName
A
X
B
Y
I can't figure it out. Any help would be appreciated.
Thank you very much!
EDIT:
so far I am trying to modify the following query to do the job:
SELECT * FROM
table1.roleId
INNER JOIN
table2.roleId
ON table1.roleId = table3.roleId
INNER JOIN
table2.userId
ON table2.userId = table3.roleId
Just join all tables together with following conditions:
Table 2 userId = Table 1 userId
Table 3 roleId = Table 1 roleId
Then select only those values which are relevant to you.
I think this should do the job:
select t2.userName, t3.roleName
from table2 t2
join table1 t1 on t2.userId = t1.userId
join table3 t3 on t3.roleId = t1.roleId;
The query above uses INNER JOINs to get the result.
If there are userIds without a roleId in Table 1, FULL OUTER JOIN would also return userNames with empty roleNames if such entries are present.

Counting and grouping columns with multiple JOIN's

I have 3 tables:
Table A
----------------------------
id | data
----------------------------
1 nothing important
2 nothing important
3 nothing important
Table B
-----------------------------------------
id | table_a_id | table_c_id
-----------------------------------------
1 1 1
2 1 2
3 1 3
Table C
----------------------------
id | name
----------------------------
1 Bob Saget
2 Neil deGrasse Tyson
3 Mike Tyson
Table B and C are 1:1 relationship, whereas Table A and Table B are a one to many relationship.
I want to get a count of the JOINed rows in Table B.
Here is my attempt that does not work:
SELECT count(*) AS count, tC.name FROM table_a tA
LEFT JOIN table_b tB ON tA.id = tB.table_a_id
LEFT JOIN table_c tC ON tC.id = tB.table_c_id
GROUP BY tA.id, tC.name
This is what I'm hoping the result will look like:
Result
----------------------------
count | name
----------------------------
3 Bob Saget
3 Neil deGrasse Tyson
3 Mike Tyson
Count represents the number of rows that were JOINed from Table B. In this case, 3 rows from table B carry a foreign key for table A.
Use can use this fiddle.
If you want to count how many rows in a relate to each c, then c has to come first in the from clause, followed by left joins to the other tables.
Also, you need to a.id from the group by clause - otherwise, you would probably get just one (or zero) row per group.
Other things to consider:
you are left joining, so you should count something from a rather than just count(*), so you get 0 for unmatched rows (instead of 1 with count(*))
I would recommend grouping by the primary key of c rather than on the name, in case two different ids might have the same name
So:
SELECT count(a.id) AS count, c.name
FROM table_c c
LEFT JOIN table_b b ON c.id = b.table_c_id
LEFT JOIN table_a a ON a.id = b.table_a_id
GROUP BY c.id

Select Multi rows from table B according to array value in table A

I have two tables (A, B), B table has a column Fruit which stores id values of table A rows as array, how can I output IN ONE SELECT STATEMENT the title of each id in table B, like that:
Table B :
id title
1 Apple
2 Orange
Table A :
id Fruit
1 1,2
result:
A.id A.Fruit
1 Apple,Orange
SELECT a.id, GROUP_CONCAT(b.title)
FROm tableA a
LEFT JOIN tableB b
ON FIND_IN_SET(b.id , a.Fruit)
GROUP BY a.id
SELECT Fruit FROM Table B WHERE Fruit IN (SELECT Fruit FROM Table A); I don't know if this will work for you but I hope it helps. You will probably need to use Subqueries.

need an efficient query for selecting from two tables

One table A, looks like this:
table A:
==========
ID NAME
1 Ted
2 John
3 Sandy
4 Robert
5 Helen
table B:
=========
CONTRIBUTION CONTRIBUTOR_ID
100 1
200 3
150 3
270 2
30 1
Assuming table B is very big and table A is small, I would like to pseudo iterate on this.
- take first ID from table A
- search for the first occurrence in table B, if found add to result
- if not continue to next ID in table A.
- repeat until end of table A
I would like a list of all ID's from table A that exist in table B
So the result here would be:
1
2
3
Of course, the tables are properly indexed.
any idea how to write this efficiently in MySQL?
Thanks
Or just simply
select distinct contributor_id
from table B
select distinct ID from tableA inner join tableB
on table.ID=tableB.CONTRIBUTOR_ID
Try this:
select tA.ID
from tableA tA inner join tableB tB on tA.ID = tB.CONTRIBUTOR_ID
group by tA.ID
the query
SELECT * from A,B where A.ID = B.CONTRIBUTOR_ID
would select all rows with an existing ID in both tables. It would leave out all rows in A which never have contributed (not exist in B)
EDIT:
to only get the IDs of those who ever contributed, try:
SELECT ID FROM A WHERE EXISTS (SELECT CONTRIBUTOR_ID FROM B where ID = CONTRIBUTOR_ID)
If table B can contain contributors that don't exist in table A, then I suggest trying:
select tA.ID
from tableA tA
inner join (select distinct contributor_id from tableB) tB
on tA.ID = tB.CONTRIBUTOR_ID
(assuming you have an index on Contributor_ID on tableB)
Can be done by using subquery
Select distinct ID from A where ID in(select CONTRIBUTOR_ID from B)

MySql find out if record's id is in another table (in one query)

lets say I have 2 tables:
Table 1: (customers)
------------------------------------------
id | name | etc... | etc..
Table 2: (blockList)
------------------------
id
I want to know if each customer exists or not in blockList table as I'm looping thru customers table (in a single query, as a seperate field)
like this: SELECT * FROM customers, blockList ORDER BY id DESC
You need to use join, example :
SELECT c.*, b.id AS id_blocklist
FROM customers AS c
LEFT JOIN blocklist AS b ON b.id = c.id
ORDER BY c.id DESC
If you want only records in blocklist, use INNER JOIN
you must specify tables connection
SELECT * FROM customers as c, blockList as b WHERE c.id = b.id ORDER BY id DESC