SQL: join table showing null value as well - mysql

i have table like this:
table1:
id | item_name | entered_by | modify_by
1 | banana | 2 | 1
2 | apple | 4 | 3
3 | orance | 1 | 1
4 | pineapple | 5 | 3
5 | grape | 6 | 1
table2:
id | username
1 | admin
2 | jack
3 | danny
4 | dummy
5 | john
6 | peter
the query is work fine to select if the entered_by or modify_by do have value:
SELECT t1.id, t1.item_name,
t2enteredBy.username enteredBy,
t2modifyBy.username modifyBy
FROM table1 t1
JOIN table2 t2enteredBy ON t1.entered_by = t2enteredBy.id
JOIN table2 t2modifyBy ON t1.modify_by = t2modifyBy.id
problem: if one of the modifiy_by or entered_by field have null value, the row is now showing out, i need it to show it out as '-' if it has null value rather than hide the row completely.
SQLFIDDLE HERE

Try this out:
SELECT t1.id, t1.item_name,
COALESCE(t2enteredBy.username, '-') enteredBy,
COALESCE(t2modifyBy.username, '-') modifyBy
FROM table1 t1
LEFT JOIN table2 t2enteredBy ON t1.entered_by = t2enteredBy.id
LEFT JOIN table2 t2modifyBy ON t1.modify_by = t2modifyBy.id
Fiddle here.
You need a left join to return those rows with null values. Then the coalesce will make sure that they are replaced by the given string if they are null.

Try this - use LEFT JOIN instead of JOIN
SELECT t1.id, t1.item_name,ifnull(t2enteredBy.username,'-') enteredBy,
ifnull(t2modifyBy.username,'-') modifyBy
FROM table1 t1
LEFT JOIN table2 t2enteredBy ON t1.entered_by = t2enteredBy.id
LEFT JOIN table2 t2modifyBy ON t1.modify_by = t2modifyBy.id
SQL Fiddle Here

Related

Display child and parent relationship (if any) in a same table

I have this table
| id |parent|name|
| 1 | NULL | E |
| 2 | NULL | B |
| 3 | 5 | U |
| 4 | 5 | X |
| 5 | NULL | C |
| 6 | NULL | A |
I would like the list, ordered by parent's name, of all ID whether they have a parent or not:
| id |parent|name|has_child|
| 6 | NULL | A | 0 |
| 2 | NULL | B | 0 |
| 5 | NULL | C | 1 |
| 3 | 5 | U | 0 |
| 4 | 5 | X | 0 |
| 1 | NULL | E | 0 |
Is it possible?
I have tried many things but never get the proper answer, and I don't really know how to add the 'has_child' column
SELECT
t1.parent,
t2.name
FROM tablename AS t1
INNER JOIN
(
SELECT MIN(id) AS id, parent
FROM tablename
GROUP BY parent
) AS t22 ON t22.id = t1.id AND t1.parent = t22.parent
INNER JOIN tablename AS t2 ON t1.parent = t2.id;
I would use a self join here:
SELECT DISTINCT
t1.id,
t1.parent,
t1.name,
1 - ISNULL(t2.id) has_child
FROM tablename t1
LEFT JOIN tablename t2
ON t1.id = t2.parent
ORDER BY
t1.id;
The join condition used here, which matches a given record as a parent to one or more children, is that the current id is also the parent of some other record(s). Note that we need SELECT DISTINCT here, because a given parent might match to more than one child record.
You can use a self join -- because you want the name of the parent and not the id -- and coalesce() for ordering:
select t.*,
(case when exists (select 1 from t tc where tc.parent = t.id)
then 1 else 0
end)
from t left join
t tp
on t.parent = tp.id
order by coalesce(tp.name, t.name), -- group rows by the parent, if any
(tp.name is null) desc, -- put parent first
t.name; -- order by children
I hope that you find this answer a little bit useful. The subquery gets the distinct id of parents and excludes the blanked fills.
SELECT *,
CASE WHEN id IN (SELECT DISTINCT parent
FROM tablename
WHERE parent IS NOT NULL)
THEN '1' ELSE '0'
END AS has_child
FROM tablename
ORDER BY name;
SELECT t1.id, t1.parent, t1.name, MAX(t2.parent is not null) has_child
FROM table t1
LEFT JOIN table t2 ON t1.id = t2.parent_id
GROUP BY t1.id, t1.parent, t1.name

how to query display all data when not all related

I want to ask ..
If i have data but data related in another table
but i want the output is display all data
usually if data related in another table, I using INNER JOIN but the output just data have a relation, if dont have relation, its not display .. IF I using LEFT JOIN or RIGHT JOIN not all data displayed .. IF I using UNION data duplicated
this just example field .. field in real so many
TABLE A
ID | NAMA |
----------------------
1 | Rina |
2 | Deni |
3 | Muti |
4 | Sina |
5 | Sasa |
TABLE B
ID | Rumah |
----------------------
1 | Jabar |
2 | Jateng |
3 | Jatim |
OUTPUT THAT I WANT
ID | NAMA | Rumah
----------------------------------
1 | Rina | Jabar
2 | Deni | Jateng
3 | Muti | Jatim
4 | Sina | -
5 | Sasa | -
short version:
SELECT COALESCE(a.ID, t2.ID),
COALESCE(a.NAMA, '-')
COALESCE(b.Rumah, '-')
FROM TableA a
LEFT JOIN TableB b
ON a.ID = b.ID
RIGHT JOIN TableB t2
ON a.ID = b.ID
If I understand your problem correctly, then a given ID might only have a first or last name, but not both. In this case, simply doing a left or right join will result in the loss of data. One approach here is to do a full outer join between your two tables on the ID, and then use COALESCE to handle possibly missing data appropriately.
SELECT COALESCE(t1.ID, t2.ID) AS ID,
COALESCE(t1.NAMA, '-') AS NAMA,
COALESCE(t2.Rumah, '-') AS Rumah
FROM TableA t1
LEFT JOIN TableB t2
ON t1.ID = t2.ID
UNION
SELECT COALESCE(t1.ID, t2.ID),
COALESCE(t1.NAMA, '-')
COALESCE(t2.Rumah, '-')
FROM TableA t1
RIGHT JOIN TableB t2
ON t1.ID = t2.ID

Select last record of each person

I have the following tables
tbl_investors
id | first_name | last_name |
---------------------------------------
1 | Jon | Cold |
2 | Rob | Ark |
3 | Rickon | Bolt |
tbl_investors_ledger
id | investor_id | amount |
------------------------------------
1 | 1 | 500 |
2 | 2 | 200 |
3 | 2 | 250 |
4 | 2 | 300 |
5 | 3 | 10 |
6 | 1 | 550 |
7 | 3 | 20 |
I just want to return all investors with their latest amount. Ex, Jon Cold with 550, Rob Ark 300 and Rickon Bolt 20, alphabetically with their last name.
I have an existing query but it will not return the latest amount of the investor. Can someone help me what i'm doing wrong?
SELECT t1.*, t2.*
FROM ".tbl_investors." t1
LEFT JOIN ".tbl_investors_ledger." t2
ON t1.id = t2.investor_id
LEFT JOIN (SELECT t.investor_id, max(t.id) as tid
FROM ".tbl_investors_ledger." t ) tt
ON tt.investor_id = t2.investor_id AND tt.tid = t2.id
GROUP BY t2.investor_id
ORDER BY t1.last_name
You can use GROUP_CONCAT and SUBSTRING_INDEX together
SELECT I.*
, SUBSTRING_INDEX(GROUP_CONCAT(L.amount ORDER BY L.id DESC), ',', 1) AS LastAmount
FROM tbl_investors AS I
LEFT JOIN tbl_investors_ledgers AS L
ON L.investor_id = I.id
GROUP BY I.id
ORDER BY I.last_name
Here a demo from SQLFiddle, many thanks to #zakhefron :)
Try this;)
SELECT t1.*, t2.*
FROM tbl_investors t1
LEFT JOIN tbl_investors_ledger t2
ON t1.id = t2.investor_id
INNER JOIN (
SELECT t.investor_id, max(t.id) as tid
FROM tbl_investors_ledger t GROUP BY t.investor_id) tt
ON tt.investor_id = t2.investor_id AND tt.tid = t2.id
ORDER BY t1.last_name
SQLFiddle DEMO
And check related OP Retrieving the last record in each group and this blog How to select the first/least/max row per group in SQL, you can find more solutions for your question.

mysql select query missing a row

I have a mysql table like this
+---------+----------------+-------+------------+
| dept_id | parent_dept_id | level | name |
+---------+----------------+-------+------------+
| 1 | 0 | 0 | Company |
| 2 | 1 | 1 | HR |
| 3 | 1 | 1 | Finance |
| 4 | 1 | 1 | Operations |
| 5 | 4 | 2 | Sales |
| 6 | 2 | 2 | Training |
+---------+----------------+-------+------------+
I am trying to retrieve all departments and their parent departments. But what I also want is to retrieve the deprtment with ID 1. This guy is the top department and does not have a parent, all I want is a null in the department name.
So far I have this query
select
d1.dept_id,
d2.name as parent_name,
d1.parent_dept_id,
d1.level,
d1.name
from
tdept d1,
tdept d2
where
d1.parent_dept_id = d2.dept_id;
Currently, all departments get retrieved except for the top one, how can I achieve this?
Try using the LEFT OUTER JOIN (The LEFT JOIN keyword returns all rows from the left table (table1), with the matching rows in the right table (table2). The result is NULL in the right side when there is no match.)
select
d1.dept_id,
d2.name as parent_name,
d1.parent_dept_id,
d1.level,
d1.name
from tdept d1
Left outer join tdept d2
where d1.parent_dept_id = d2.dept_id;
Ok, I got it working, I did this:
select
d1.dept_id,
d2.name as parent_name,
d1.parent_dept_id,
d1.level,
d1.name
from
tdept d1 left outer join tdept d2 on d1.parent_dept_id = d2.dept_id;
Thanks Nadeem_MK for putting me in the right direction
Crouz
Try something like this:
select
t1.dept_id,
t2.name,
t1.parent_dept_id,
t1.level,
t1.name
from tdept t1
Left outer join tdept t2
ON t1.parent_dept_id = t2.dept_id;

Two SELECT's in one query in the same table

I have one table:
id | parent_id | name
1 | NULL | audi
2 | 1 | a5
3 | 1 | a6
4 | NULL | opel
5 | 4 | astra
6 | 4 | vectra
I want get name of record and name of record's parent.
One example is to fetch the name for id=5 and its parent name.
id | name | parent_name
5 | astra | opel
What would be SQL query for this?
My query:
SELECT name, parent_id FROM `cats` WHERE `id` = 5 OR `id` =
cats.parent_id LIMIT 0 , 30
didn't work.
You can use the below query:
SELECT T1.id, T1.name, T2.name as parentname
FROM TABLE1 T1
INNER JOIN TABLE1 T2 ON T1.id = T2.parent_id
WHERE T2.ID = 5
SQL FIDDLE
This work's if you have only 1 parent at a time (no recursion):
SELECT a.name name, b.name parent_name
FROM tablexyz a,tablexyz b where
a.id=5 AND a.parent_id=b.id;
This worked fine for me. Please check it out.
select a.id,a.name,b.name as parent from cats a,cats b where b.id=a.parent_id;
You can add any other conditions too. (make sure to use the correct table identifier; a or b)