MySQL join on whichever column is not null - mysql

I have a table with a bunch of columns, but we only need to look at two of them. I'm trying to join another table on this table, but all we know about these two columns is that one will be null and the other won't:
client_id | note_id
The main table wants to join client_id (if not null) on clients.id OR note_id on notes.id if clients.id is null.

This will work for you. This is very basic query I wrote. Make changes if required.
SELECT * FROM YOUR_TABLE t
LEFT OUTER JOIN clients c ON t.client_id = c.id
LEFT OUTER JOIN notes n ON t.note_id = n.id
WHERE c.id IS NOT NULL OR n.id IS NOT NULL

Assuming there are 3 tables involved (the main table that contains client_id and note_id columns, clients table, and notes table), you can use a query such as this:
(select *
from mainTable inner join clients on mainTable.client_id = clients.id)
union
(select *
from mainTable inner join notes on mainTable.note_id = notes.id
where mainTable.client_id is NULL);
The above query contains 2 queries where each query will output rows where the joining column is not null. The results are then combined using union.

You can use coalesce in the join on clause. See demo here:
http://sqlfiddle.com/#!9/99911/2. If client id is null then use note id to join table1 and table2.
Select t1.client_id, t1.note_id,t2.client_id, t2.note_id
From table1 t1
Join table2 t2
on coalesce(t1.client_id, t1.note_id) =coalesce(t2.client_id, t2.note_id)

Related

How to join 3 table and perform union in a subquery?

There are four table Bill_entry,Customer,Chit,Cash. I want to join table Customer with table Bill_entry in following query where they have common column customer_id. Goal here is by using customer_id i want to print customer_name too in one query.
i have tried but couldn't get correct syntax
Initial code before including Customer table :
SELECT Bill_entry.*
FROM (SELECT * FROM Chit UNION SELECT * FROM Cash) as t1 RIGHT JOIN
entry
ON (Bill_entry.bill_no = t1.bill_no)
WHERE t1.bill_no IS NULL
MY tries :
SELECT Bill_entry.*, Customer.customer_name
FROM ((SELECT * FROM Chit UNION SELECT * FROM Cash) as t1 RIGHT JOIN entry ON (Bill_entry.bill_no = t1.bill_no) WHERE t1.bill_no IS NULL)customer where Bill_entry.customer_id = Customer.Customer_id
Just add in another JOIN:
SELECT e.*, cu.customer_name
FROM bill_entry e LEFT JOIN
(SELECT * FROM Chit
UNION ALL -- assume you don't want to remove duplicates
SELECT * FROM Cash
) c
entry e
ON e.bill_no = c.bill_no LEFT JOIN
Customer cu
ON cu.customer_id = e.Customer_id
WHERE c.bill_no IS NULL;
Note some changes.
The UNION --> UNION ALL. I assume you don't want to remove duplicates or incur the overhead for trying to remove them.
RIGHT JOIN --> LEFT JOIN. It is usually much simpler to think about LEFT JOINs -- keep all the rows in the first table and then matching rows in the others.
The JOIN conditions are all in ON clauses, not the WHERE clause.

Select multiple row even first table is null mysql

I've encountered a problem on my wordpress website. I have multiple select mysql query from one page then decided to merge because it's in from same table. But the problem is when first selected table is null, all other row is affected and becomes null. And if the first select table is has a value, then the others selected rows will be display.
Here's my sample mysql query code:
SELECT u1.meta_value as name, u2.meta_value as birthday,
u3.meta_value as place
FROM wp_usermeta u1
LEFT JOIN wp_usermeta u2 ON u1.user_id = u2.user_id
LEFT JOIN wp_usermeta u3 ON u1.user_id = u3.user_id
WHERE u1.user_id = 1092
AND u1.meta_key = 'name' AND u2.meta_key = 'birthday' AND u3.meta_key = 'place'
you can use union and 2nd query as a right join below is sample code
The LEFT JOIN keyword returns all records from the left table
(table1), and the matched records from the right table (table2). The
result is NULL from the right side, if there is no match.
The UNION operator is used to combine the result-set of two or more
SELECT statements.
Each SELECT statement within UNION must have the same number of
columns
The columns must also have similar data types
The columns in each SELECT statement must also be in the same order
select t1.* from t1 left join t2 on t1.id=t2.id
union
select t2.* from t1 right join t2 on t1.id=t2.id

SQL : Multiple left joins with similar tables

I have the following tables:
TABLE A
id
info
TABLE B
f_id
question
choices
TABLE C
f_id
question
lines
The id from the Table A always match a f_id from either Table B or C, but never both. I want to join Table B and Table C on table A only when it matches so I would get a table with the following columns :
id | info | question | choices | lines
where all rows are filled in the question column, some are NULL in the column choices and some are NULL in the column lines.
What I tried is to do two consecutive left joins, but the second one overrides the first so all the rows that doesn't match in Table C (second left join) get a NULL value in the question column.
Is there a way to do a query that will not override previously joined data with NULL values? I'm working with Laravel Eloquent, so any of raw SQL or Eloquent Query would help me.
UNION B and C and then INNER JOIN A to those results.
SELECT s1.f_id, s1.question, s1.choices, s1.lines
FROM
(
SELECT f_id, question, choices, lines = null
FROM B
UNION
SELECT f_id, question, choices = null, lines
FROM C
) s1
INNER JOIN A ON s1.f_id = A.id
but never both
Good luck with that.
id | info | question | choices | lines
SELECT a.id, a.info, b.question, b.choices, '' AS lines
FROM tableA as A
LEFT JOIN tableB AS b
ON a.id=b.f_id
UNION
SELECT a.id, a.info, c.question, '', c.lines
FROM tableA as A
INNER JOIN tableC AS c
ON a.id=c.f_id
You could use UNION to combine two different queries.
SELECT
`id`, `info`, `question`, `choices` AS `lines`
FROM
`TABLE_A` INNER JOIN
`TABLE_B` ON `TABLE_A`.`id` = `TABLE_B`.`f_id`
UNION
SELECT
`id`, `info`, `question`, `lines`
FROM
`TABLE_A` INNER JOIN
`TABLE_C` ON `TABLE_A`.`id` = `TABLE_C`.`f_id`
Make sure to use JOIN or INNER JOIN (JOIN defaults to INNER JOIN on MySQL) AND not LEFT JOIN, RIGHT JOIN, OUTER JOIN otherwise you'll end up with partially filled results.

Select all which is not in table

I have a table in MySQL where all employees are listed. I have another where all employees are listed which have to work on a specific day. And now I want to select all employees which have free (or at least are NOT listed in the work-table).
In this fiddle you can see my schema.
A code like SELECT * FROM pf_mitarbeiter WHERE NOT LISTED AS employeeID IN pf_tagesplan_zuteilungen would be super awesome. But I take other versions as well too.
Thank you guys!
Use a LEFT JOIN to join pf_tagesplan_zuteilungen on employeeID with a condition that there's no rows matching pf_mitarbeiter:
SELECT t1.*
FROM pf_mitarbeiter t1
LEFT JOIN pf_tagesplan_zuteilungen t2 ON t2.employeeID = t1.ID AND t2.date = CURDATE()
WHERE t2.ID IS NULL
select *
from A
where not exists
(
select 1
from B
where a.key = b.key
)
Use a LEFT OUTER JOIN to join the two tables. By doing so, you can select all the rows from the pf_mitarbeiter table even though there is no related row in the pf_tagesplan_zuteilungen table.
SELECT
S.*
FROM
pf_mitarbeiter S
LEFT OUTER JOIN pf_tagesplan_zuteilungen T ON (S.ID = T.employeeID)
WHERE
T.ID IS NULL
;
The IS NULL condition restricts the join to only return pf_mitarbeiter rows where there is no pf_tagesplan_zuteilungen row with a matching employeeId.

Select data from a right unknown table, how to?

I have three tables, a user-table and two user-type tables. The user-type tables have a field UserId so they can be linked. The question/problem now is that I have an entry from the user-table. What is the best way to get the data from the right table?
Is it correct to use 2 right joins or can this be done more elegantly?
Yes, but I'd use LEFT OUTER JOIN. That is, you want the row from UserTable, and then possibly a row from the appropriate UserType table, if such a row exists.
Here's an example:
SELECT ...
FROM UserTable u
LEFT OUTER JOIN UserType1 t1 ON u.userid = t1.userid
LEFT OUTER JOIN UserType2 t2 ON u.userid = t2.userid
Presumably at most one of the user-type tables has an entry. So either t1.* or t2.* will be a single row of all NULLs, and you won't get multiple rows due to Cartesian product.
Besides the LEFT JOIN, there's also the UNION which needs some more work to show the different columns that the 2 tables might have:
SELECT u.*
, t1.colA, t1.colB, ..., t1.colZ --- columns in both tables 1 and 2
, t1.col_1a, t1.col_1b, t1.col_1c --- columns in table 1 only
, NULL AS col_2a --- columns in table 2 only
, NULL AS col_2b
FROM UserTable u
LEFT OUTER JOIN UserType1 t1 ON u.userid = t1.userid
UNION ALL
SELECT u.*
, t2.colA, t2.colB, t2.colC --- columns in both tables 1 and 2
, NULL AS col_1a --- columns in table 1 only
, NULL AS col_1b
, NULL AS col_1c
, t2.col_1a, t2.col_1b --- columns in table 2 only
FROM UserTable u
LEFT OUTER JOIN UserType2 t2 ON u.userid = t2.userid