to fetch data from 2 tables - mysql

I am using MySQL to fetch data from 2 tables.
I want to fetch value 116 from column C of table 1 by matching column B of table 1 with column H of table2. If I apply join between the two, I get row second of table 1 as a result. But I want to get value 116 by matching values between the two. How can this be achieved in MySQL?

You seem to want a simple join:
select t2.*, t1.c
from table2 t2 join
table1 t1
on t2.h = t1.b;
You just need to specify the correct columns for lining up the two tables.

INNER JOIN will be good for you, like the following :
SELECT
table2.F as F, table1.A as A, table1.C as C
FROM table2
INNER JOIN table1
ON ( table1.B = table2.H )
or a LEFT JOIN in case C has a null value.
SELECT
table2.F as F, table1.A as A, table1.C as C
FROM table2
LEFT JOIN table1
ON ( table1.B = table2.H )

Related

Join table 1 with 2. If no values found in table 2 fetch from table 3

I want to join table 1 T1 with table 2 T2 on LEID and pull some values ISIC Code from T2 for the joined values.
If I don't get values ISIC Code from T2 for the LEID, I want to check the same from table 3 basis the same joining attribute.
Presumably, you want two left joins. I think it would look like this:
select . . ., coalesce(t2.isic, t3.isic) as isic
from t1 left join
t2
on t1.leid = t2.leid left join
t3
on t1.leid = t3.leid and t2.leid is null;

Oracle Select Query based on Column value

I have two tables lets say
Table A
columns id , name address
Table B
columns id , age, import_date
The Table B id is a reference key of Table A.
Now I want to return results from A & B but if the record is not in B I still want to see the record so for this I use left outer join
Select * from A a left join B b
on a.id = b.id
Now even I don't have record in B I still get the record.
Table B may contain duplicate ids but unique import_date.
Now I want to results in a way that if there is duplicate id in table B then I want to get the records only where import_date is as of today.
I still want to get the records for ids which are not there but if the ID is there in table B then I want to apply above condition.
I hope someone can help me with this.
Sample data
Table A
01|John|London
02|Matt|Glasgow
03|Rodger|Paris
Table B
02|22|31-AUG-2015
02|21|30-AUG-2015
02|23|29-AUG-2015
The query will return
01|John|London|null|null|null
02|Matt|Glasgow|22|31-Aug-2015
03|Rodger|Paris|null|null
You almost got the solution. Just add one more condition like below
Select a.id,a.name,a.address,b.age,b.import_date
from tablea a left join tableb b
on a.id=b.id and b.import_date=trunc(sysdate)
order by a.id;---This line optional
Check the DEMO HERE
SELECT *
FROM Table_A t1 LEFT OUTER JOIN Table_B t2 ON t1.id=t2.id UNION
SELECT *
FROM Table_A t1 LEFT OUTER JOIN Table_B t2 ON t1.id=t2.id
GROUP BY t2.import_date
HAVING t2.import_date=CURDATE();

3 tables and 2 left joins

Query 1:
SELECT sum(total_revenue_usd)
FROM table1 c
WHERE c.irt1_search_campaign_id IN (
SELECT assign_id
FROM table2 ga
LEFT JOIN table3 d
ON d.campaign_id = ga.assign_id
)
Query 2:
SELECT sum(total_revenue_usd)
FROM table1 c
LEFT JOIN table2 ga
ON c.irt1_search_campaign_id = ga.assign_id
LEFT JOIN table3 d
ON d.campaign_id = ga.assign_id
Query 1 gives me the correct result where as I need it in the second style without using 'in'. However Query 2 doesn't give the same result.
How can I change the first query without using 'in' ?
The reason being is that the small query is part of a much larger query, there are other conditions that won't work with 'in'
You could try something along the lines of
SELECT sum(total_revenue_usd)
FROM table1 c
JOIN
(
SELECT DISTINCT ga.assign_id
FROM table2 ga
JOIN table3 d
ON d.campaign_id = ga.assign_id
) x
ON c.irt1_search_campaign_id = x.assign_id
The queries do very different things:
The first query sums the total_revenue_usd from table1 where irt1_search_campaign_id exists in table2 as assign_id. (The outer join to table3 is absolutely unnecessary, by the way, because it doesn't change wether a table2.assign_id exists or not.) As you look for existence in table2, you can of course replace IN with EXISTS.
The second query gets you combinations of table1, table2 and table3. So, in case there are two records in table2 for an entry in table1 and three records in table3 for each of the two table2 records, you will get six records for the one table1 record. Thus you sum its total_revenue_usd sixfold. This is not what you want. Don't join table1 with the other tables.
EDIT: Here is the query using an exists clause. As mentioned, outer joining table3 doesn't alter the results.
Select sum(total_revenue_usd)
from table1 c
where exists
(
select *
from table2 ga
-- left join table3 d on d.campaign_id = ga.assign_id
where ga.assign_id = c.irt1_search_campaign_id
);

MySQL - Join two tables, different columns in table 1 on the same column in table 2

I'm trying to join a different column (part_type_n (where n ranges from 1 to 54)) on Table1 with the same column (id, primary, autoinc) on Table2.
Schema:
Table1
==============
part_type_1
.
.
.
part_type_54
Table2
=============
id
I tried the obvious query (php generated, looping through n from 1 to 54), omitted repetitive stuff in ...:
SELECT * FROM Table1 JOIN Table2 on (Table1.part_type_1=Table2.id), ..., (Table1.part_type_54=Table2.id)
I receive this error:
1066 - Not unique table/alias: 'Table2'
How do I join these two tables?
You will have to join the table on it self again multiple times.
SELECT * FROM table1 t1
INNER JOIN table2 t2 on t2.Id=t1.part_type_1
INNER JOIN table2 t3 on t3.id = t1.part_type_54;
Hope this helps!
As an alternative to writing a query with 54 table aliases, you could consider joining to the table once - like so:
select ...
from Table1 t1
join Table2 t2
on t2.id in (t1.part_type_1, t1.part_type_2, ... t1.part_type_54)
It worked for me to get my required result as one row of which matches various categories all stored in one table column.
Query
SELECT cm3.*, xp.post_title,GROUP_CONCAT(DISTINCT sc.name) AS cate_list
FROM `xld_posts` xp
JOIN course_map cm0 ON cm0.course_id = xp.ID
JOIN course_map cm1 ON cm1.course_id = cm0.course_id AND cm0.id = 3
JOIN course_map cm2 ON cm2.course_id = cm1.course_id AND cm1.id = 6
JOIN course_map cm3 ON cm3.course_id = cm2.course_id AND cm2.id = 11
JOIN subject_category sc ON cm3.id = sc.id
GROUP by post_title ORDER BY post_title
Note: the categories values 3, 6, and 7 are got from form sumbit. Thus if your form has more than three or less your query should dynamically created and join each table with previous table.
:) Happy if any one felt useful.

Linking 3 tables together in MySQL

I'm trying to link 3 database table together through one MySQL request.
Database structure :
Table1 :
table1_id (exemple: 1)
table1_name (exemple: hello world)
Table2 :
table2_id (exemple: empty)
table2_name (exemple: empty)
Table3 :
table3_id (exemple: 1)
table3_name (exemple: random_name
MySQL Request
SELECT * FROM table1 AS a, table2 AS b, table3 AS c
WHERE a.table1_id = b.table2_id
AND a.table1_id = c.table3_id AND table3_name = "random-name"
Problem
The previous request won't display any result because table2 is empty. Do you have an idea how I could get the data coming from table 1 & 2, letting table3's fields empty without using two requests ?
You should change your request to use a LEFT JOIN instead of an INNER JOIN:
select *
from table1 t1
left join table2 t2
on t1.table1_id = t2.table2_id
left join table3 t3
on t1.table1_id = t3.table2_id
and t3.table3_name = 'random-name'
The INNER JOIN produces a set of data if the id exists in all tables. The LEFT JOIN will return records from table1 even if there are no records in table2 or table3.
If you need help learning about join syntax, here is a great visual explanation of joins
Couple thoughts:
Don't use SELECT *, name the columns explicitly
Don't use implicit join syntax; use ON clauses
To preserve rows that do not "match" on a join condition, use a LEFT OUTER JOIN
Therefore, try this:
SELECT a.table1_id, a.table1_name
, b.table2_id, b.table2_name
, c.table3_id, c.table3_name
FROM table1 AS a
LEFT OUTER JOIN table2 AS b
ON b.table2_id = a.table1_id
JOIN table3 AS c
ON c.table3_id = a.table1_id
WHERE c.table3_name = "random-name"