Select multiple row even first table is null mysql - 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

Related

Join two mysql tables with Where Cluase

I need to join MySQL tables.
but when I apply where clause on the second table, query return no record.
I want a query that returns all records of the 1st table and joins with 2nd table data on certain conditions and if the condition does not find records in the second table then it appends null in the result.
Please see the attached picture for detail.
enter image description here
SELECT *
FROM `Table1`
JOIN `Table2` ON `Table1`.`id1` = `Table2`.`id2`
WHERE `Table2`.`department_id` = 2;
Use a left join, and move the where criteria to the join condition:
SELECT *
FROM Table1 t1
LEFT JOIN Table2 t2 ON t1.id1 = t2.id2 AND t2.department_id = 2;

MySQL join on whichever column is not null

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)

MySQL Left Outer Join not returning NULL values for COUNT(*)

I am attempting to left outer join 2 tables: TABLE1 contains a list of users and TABLE2 contains a list of forms completed by users. I want to display the count of forms by user, created after a given date, where status equals COMPLETED.
The following query is working but not displaying NULL values (which I need):
select TABLE1.USERNAME, count(TABLE2.FORMS)
from TABLE1
left outer join TABLE2 on (TABLE2.USERID = TABLE1.USERID)
and TABLE2.STATUS = COMPLETED
where TABLE2.DATE > 20140801
group by TABLE1.USERNAME;
What do I need to do to include users with NULL counts i.e. no forms in TABLE2?
Thanks
You need to move the condition in the where clause to the on clause:
select TABLE1.USERNAME, count(TABLE2.FORMS)
from TABLE1 left outer join
TABLE2
on (TABLE2.USERID = TABLE1.USERID) and TABLE2.STATUS = COMPLETED and
TABLE2.DATE > 20140801
group by TABLE1.USERNAME;
In rows that don't match, the table2 columns are set to NULL. That, in turn, causes your where clause to fail (because comparisons to NULL return NULL, which is treated as false).
When using left outer join, filters on the second table go in the on clause. Filters on the first table go in the where clause.

Left Join in Mysql?

This is my first Table.
Then the second one is
Now I am trying to do Left join like this
SELECT t1.StackID FROM t1 LEFT JOIN t2 on t1.StackID=t2.StackID
Output
I am confused here, Is this the correct output? Is not this supposed to return only the 5 rows which is present in left side table.
It's correct output. You are doing LEFT JOIN, so for every record in LEFT table DBMS will 'concatenate' the corresponding RIGHT table record(-s) (and NULL, if there's no corresponding record for the JOIN condition; also remember, that if there are more that 1 corresponding record - all will be joined - this issue is why you're getting not only 5 records from 1-st table).
The thing you're trying to achieve should be done by either DISTINCT modifier, i.e.
SELECT DISTINCT t1.StackID FROM t1 LEFT JOIN t2 ON t1.StackID=t2.StackID;
More about JOIN in SQL you can read here.
There is a simple example of left join:-
SELECT * FROM a JOIN b LEFT JOIN c ON (c.key=a.key) LEFT JOIN d ON (d.key=a.key)
WHERE b.key=d.key;
These extra repeated values of StackId are coming from the join, just use DISTINCT to get those only 5 values:
SELECT DISTINCT t1.StackID
FROM t1
LEFT JOIN t2 on t1.StackID=t2.StackID;
SQL Fiddle Demo
Yes, this is the expected output.
To get the distinct IDs, use DISTINCT -
SELECT DISTINCT t1.StackID
FROM t1
LEFT JOIN t2
ON t1.StackID=t2.StackID
When you left joined your t2 table with t1 based on stackID, then the database will join every row of t2 with the every row of t1 which has the same stackID value. Since 1 appears in six rows in the t2 table as a stackID value, all of them are joined with the first row of t1. Similar thing is true for all other values.
To give yourself a little more understanding of how a left join works try this:
SELECT t1.StackID, t2.StackID FROM t1 LEFT JOIN t2 on t1.StackID=t2.StackID
basically a left join returns all the rows from the left table plus matching ones from the right.

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