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
Related
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
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)
I have a 3 tables in my database when I make a consult in table 1 I need to check 1 column if the value is 1 or 2, if 1 I need to do an inner join with table 2 else if the column is 2 I need to do an inner join with table 3. Can I do this all in one sql query?
table 1
id
type-check (1 or 2)
if - table 1.type-check = 1 inner join table 2
else - table 1.type-check = 2 inner join table 3
thanks, and forgive me for my bad english I'm from Brazil, and still learning.
You can't do this with inner join. But you can construct a query with left outer join:
select t1.*, coalesce(t2.col1, t3.col1) as col1
from t1 left join
t2
on t1.type_check = 1 and t1.col = t2.col left join
t3
on t1.type_check = 2 and t1.col = t3.col;
Note that you also need to select the columns from the right table in the SELECT clause.
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.
I have 2 two tables.One table contains 2 user id (like fast user and second user) in a row. Another table contains user data (like user name).
How can I retrieve 2 user name using left join?? I would like to have sql query. How can I get that result in php ??
Thanks
Foysal
Try this one,
SELECT a.fastuser, b.*,
a.secondUser, c.*
FROM table1 a
LEFT JOIN table2 b
on a.fastUser = b.userID
LEFT JOIN table2 c
on a.secondUser = c.userID
You can join the same table twice and use different alias names for tables and user_name columns:
select u1.user_name as first_username,
u2.user_name as second_username
from some_table t
left join user_table u1 on t.first_user_id = u1.user_id
left join user_table u2 on t.second_user_id = u2.user_id
Say Table1 contains user info :--id,id_c,email,age etc.
Table 2 contains user_name :--id,user_name
here id in table1 refers to id in table 1(for 1st user)
here id_c in table1 refers to id in table 2(for 2nd user)
Then query would be like
select a.user_name,b.user_name,u.email from Table1 as u
left join Table2 as a on u.id=a.id
left join Table2 as b on u.id_c=b.id
You can go through my blog-post for reference purpose
http://codebucket.co.in/for-mysql-savvy-multiple-joins-with-alias/