SQL JOIN Depending on Condition - php

My question pertains to joining data based on a specific condition.
For example, in table A there is a column known as user_type. If user_type is 1, I want to do an INNER JOIN from table B. However, if user_type is 2 in Table A, I want to do an INNER JOIN from table C.
Much appreciated if someone could iron out the syntax in PHP :-)

Use INNER JOIN with both tables as usual, and use a CASE statement just to show table B or table C result.
SELECT CASE WHEN tableA.user_type = 1 THEN tableB.user_type
WHEN tableA.user_type = 2 THEN tableC.user_type
END as user_type
FROM tableA
INNER JOIN tableB
ON tableA.id = tableB.id
INNER JOIN tableC
ON tableA.id = tableC.id

Related

mysql merge query in one output with different fields

I have four db table,
tableA, tableB, tableC, tableD
I am listing records with join. These all working fine.
My query is like following.
Select DISTINCT A.*, B.loginname, B.email, C.full_name, C.address1, C.state, C.city, D.*
FROM (
tableA AS A
INNER JOIN tableB AS B ON B.user_id = A.id
INNER JOIN tableC AS C ON C.user_id = A.id
INNER JOIN tableD AS D ON D.user_id = A.id
) WHERE A.id = '269' ORDER BY A.created_date DESC
Now, issue is i have created four another table with almost same details but there are different fields and columns. So i can not use UNION.
tableE, tableF, tableG, tableH
i want to merge output to display. And output should be ORDER BY A.created_date OR ORDER BY E.created_date
Advanced thanks...
You can use UNION ALL (or UNION, if you want duplicates removed) between two tables with differently named columns by declaring name aliases.
SELECT 'first' AS source, a,b,c,d
FROM first
UNION ALL
SELECT 'second' AS source, q AS a, r AS b, s AS c, t AS d
FROM second
This will yield a result set (virtual table) with columns named source,a,b,c,d.

Joining 3 tables with mySQL with 1 query

This has been asked and answered in different instances but all that I've seen doesn't quite work for my problem.
PROBLEM: I have 3 tables I need to pull data from at the same time to compare and retrieve information from. All 3 tables contain an 'email' column. Now, a user's email from table1 may match same user's email in both table2 AND table3, depending on user's status. OR an email from table1 will ONLY match either an email in table2 or table3, depending on user's status again. For example, a user may have a red status (user will show in table2), a blue status (user will show in table3), or both, red and blue (user will show in both, table2 and table3).
WHAT IS NEEDED: an email from table1 needs to be compared to email in table2 and table3 and return a region value for a given user, which is recorded in table2 and table3 but not in table1. I know. Delightful data architecture! Either way, I was able to JOIN table1 to table2 very successfully but I am not sure how to slap on a JOIN with table3.
Here's the query for 2 tables:
SELECT * FROM table1
INNER JOIN table2
ON table2.email = table1.email
WHERE month = 'numberHere' ORDER BY submitdate DESC
When I simply add another INNER JOIN my code doesn't break per say but it doesn't give me any rows to be displayed either. So the code below doesn't work despite the many working examples from the web:
SELECT * FROM table1
INNER JOIN table2
ON table2.email = table1.email
INNER JOIN table3
ON table3.email = table2.email
WHERE month = 'numberHere' ORDER BY submitdate DESC
To match the row from table1 in any case and the rows of other tables only if they are, you have to use LEFT JOIN, joining table2 and table3 with table1 (not table2 with table1 and table3 with tabel2):
SELECT * FROM table1
LEFT JOIN table2
ON table2.email = table1.email
LEFT JOIN table3
ON table3.email = table1.email
In this way you select values from table1 even without a match in other tables.
See more about SQL Joins: Different SQL Joins
You need to use LEFT JOIN, so that the joins will succeed even if there's no matching row in one of the tables.
SELECT * FROM table1
LEFT JOIN table2
ON table2.email = table1.email
LEFT JOIN table3
ON table3.email = table2.email
WHERE month = 'numberHere'
AND (table2.email IS NOT NULL OR table3.email IS NOT NULL)
ORDER BY submitdate DESC
The additional conditions filter out rows that have no match in either of the tables.

Which Join do i use for update when one of my table is empty for a common id?

I am using a left to update values in multiple table . I have three table a b and c . i have a common id for all three tables. But i need to update all the tables in one go even if one of the tables does not have a entry ? is it possible. which join do i use ?
You could use multiple LEFT JOINs with a GROUP BY:
SELECT *
FROM a
LEFT JOIN b ON a.id = b.id
LEFT JOIN c ON a.id = c.id
GROUP BY a.id

the whole data from tables is not coming via join query in mysql

i'm working with php mysql and there are 12 tables which contains student informations.There are 3 main table First table is registration, second is demandraft and third is creditcard.The demandraft table contains all the creditcard table fields but as empty. now i want to get the whole data from these three tables to generate my xls file but coz there are empty fields of creditcard table in demandraft table so unable to fetch the whole records from all 3 tables. there is stuid field common in all 3 tables.
Here is my join query for that:
$sql = "select * from registration
join programme on registration.id=programme.stuid
join family on registration.id=family.stuid
join address on registration.id=address.stuid
join education on registration.id=education.stuid
join extradetail on registration.id=extradetail.stuid
join workexperience on registration.id=workexperience.stuid
join demanddraft on registration.id=demanddraft.stuid
join payonline on registration.id=payonline.stuid
where (DATE(registration.createddate)>='".$term1."'
AND DATE(registration.createddate)<='".$term2."')";
Use a left join.
select *
from a join b on a.id = b.a_id
will not list lines of table a that do not appear in table b.
select *
from a left join b on a.id = b.a_id
will.
Left join might be a bit tricky when chaining many of them with multiple tables. You may have to cleverly use parentheses around joins such that the order of joins is correct.
This :
$sql = "select * from registration
left join programme on registration.id=programme.stuid
left join family on registration.id=family.stuid
left join address on registration.id=address.stuid
left join education on registration.id=education.stuid
left join extradetail on registration.id=extradetail.stuid
left join workexperience on registration.id=workexperience.stuid
left join demanddraft on registration.id=demanddraft.stuid
left join payonline on registration.id=payonline.stuid
where (DATE(registration.createddate)>='".$term1."'
AND DATE(registration.createddate)<='".$term2."')";
Should do the trick

How to get a LEFT JOINED query result without all of the NULLs

I am LEFT JOINing multiple tables as such:
SELECT * FROM table1
LEFT JOIN table2 ON table1.id = table2.fid
LEFT JOIN table3 ON table1.id = table3.fid
LEFT JOIN table4 ON table1.id = table4.fid
LEFT JOIN table5 ON table1.id = table5.fid
LEFT JOIN table6 ON table1.id = table6.fid
WHERE table2.id = '5184'
Reason being is simply because I tried regular JOIN and no results are returned unless at least one record exists in tables1-6 which is often not the case. As it stands this returns the info I want, but the problem is that a lot of the field names repeat and when I do a 'mysql_fetch_array()' in PHP it hides any field with a duplicate name.
What type of query can I do to avoid this? At the very least, how do I construct the query such that it doesn't include all the double fields which are NULL?
Thanks
I can't help but notice that you aren't selecting anything from your left joined tables. It's my understanding that joins are typically used to pull data from secondary tables where something in those tables matches a field in the primary table. I pulled this example from W3 schools:
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
LEFT JOIN Orders
ON Persons.P_Id=Orders.P_Id
ORDER BY Persons.LastName
And unless this is a typo, you are selecting everything from table 1 where an id column = 5186 in table 2.
You state that your query returns the info you want, but with many nulls and overwritten values. To fix this, I would restructure your query to follow the typical format shown above, and make sure you have a proper use of the left join statement.
EDIT AFTER OP COMMENTS:
You are naming a row id from table2, so I assume you should start by pulling fid from table 2 where table2.id = 5186, then left join from the other tables where table1.id = table2.fid.
SELECT table2.fid, table1.id, table3.id, [add other tables here, and revise field names to select the ones you want]
FROM table2
LEFT JOIN table1 ON table1.id = table2.fid
LEFT JOIN table 3 ON table3.fid = table1.id
[add left joins to other tables here]
WHERE table2.id = '5186'
If this doesn't help, I suggest being more specific towards what information the tables hold, and what you are searching for. A little more detail may help us help you.

Categories