need an efficient query for selecting from two tables - mysql

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)

Related

SQL to fetch data where Unique key matches but the data is different in some other columns between different tables

I have two tables of same structure as below. I am trying to write a query to compare both the tables using the Unique key which is the first column and trying to return values when there is a mismatch in the second column.
If the key is not present then no need to consider that data. only if the key is present in both the table then we have compare it.
Table A
ColumnA ColumnB
A 1
B 2
C 2
D 8
Table B
ColumnC ColumnD
A 1
B 3
C 5
F 4
For example the output of the above table when comparing Table A with B should be
B 2
C 2
and when comparing Table B with A it should be
B 3
C 5
Ideally the difference in the base table should come.
I have tried Joins and Unions but I am not able to fetch the data as mentioned above.
Since you want only those rows which has matching FK values in both the tables, we simply need to use INNER JOIN.
Now, we can simply consider the unmatching rows by using WHERE .. <> ..
When comparing Table A against Table B, we can get Table A rows only:
SELECT
tA.*
FROM tableA AS tA
JOIN tableB AS tB
ON tB.ColumnC = tA.ColumnA
WHERE tB.ColumnD <> tA.ColumnB
When comparing Table B against Table A, simply fetch the rows from Table B only:
SELECT
tB.*
FROM tableA AS tA
JOIN tableB AS tB
ON tB.ColumnC = tA.ColumnA
WHERE tB.ColumnD <> tA.ColumnB
I would do :
SELECT t.*
FROM tablea t
WHERE EXISTS (SELECT 1 FROM tableb t1 WHERE t1.cola = t.cola AND t1.colb <> t.cold);
Same would be for second version just need to swipe the table names.
use EXISTS and union all
SELECT t.*
FROM tablea t
WHERE EXISTS (SELECT 1 FROM tableb t1 WHERE t1.cola = t.cola AND t1.colb <> t.colb)
union all
SELECT t.*
FROM tableb t
WHERE EXISTS (SELECT 1 FROM tablea t1 WHERE t1.cola = t.cola AND t1.colb <> t.colb)

How to get ONLY common rows of 2 tables on 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

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.

select self join if only one resulting row

Is it possible/economical to perform a SELF JOIN of a table (for this example, my table called myTable has two columns pk and fk), and return a record if there is only one resulting record? I am thinking of something like the following, however, only_one_row() is a fictional function that would need to be replaced with something real:
SELECT fk
FROM myTable as t1
INNER JOIN myTable AS t2 ON t2.fk=t1.fk
WHERE t1.pk=1
AND only_one_row();
For instance, if myTable(id,fk) had the following records, only one record is produced, and I which to select the record:
1 1
2 1
3 2
However, if myTable(id,fk) had the following records, two '1' records are produced, and the select should not return any rows:
1 1
2 1
3 2
4 1
I could use PHP to do so, but would rather just use SQL if feasible.
Use a HAVING clause that counts the results.
SELECT fk
FROM myTable as t1
INNER JOIN myTable AS t2 ON t2.fk=t1.fk
WHERE t1.pk=1
HAVING COUNT(*) = 1
How about this:
SELECT fk
FROM myTable as t1
INNER JOIN myTable AS t2 ON t2.fk=t1.fk
WHERE t1.pk=1
GROUP BY fk
HAVING COUNT(fk) = 1

MySql join returns null on table empty

I have 2 tables: A and B. I want a query which will return results if any of the values are satisfied in either of the 2 tables. I tried using a join, but it returned null when the second table is empty and vice versa.
Table A
emp_no emp_add data
12 go nice
Table B
emp_no emp_add id
12 go 1
Required output
data id
nice 1
Similarly
Table A
emp_no emp_add data
12 go nice
Table B
emp_no emp_add id
Required output
data id
nice
SELECT A.data, B.id
FROM A left join B ON A.emp_no = B.emp_no
WHERE A.data='nice'
AND a.id='1' ;
use Left Join instead on the table that you accept null values
Try this::
Select ifnull(a.data,'') as data, ifnull(b.id,'') as id from tableA a left join tableB b on (a.emp_no=b.emp_no)
As you told that it would happen vice versa, it would better to use Outer Join in your case:
Select tblA.data,tblB.id from tableA tblA FULL OUTER JOIN tableB tblB on tblA.emp_no=tblB.emp_no
select tableA.data, tableB.id from tableA, tableB