Reputation: 25
I want to join the two tables transaction and item but some rows are returning with null values but in the database they have values. I need this query in the php I'm working with. the tables and query is below. . please help me. .
mysql> select * from item;
+----------+-------------------------+-----+--------+
| itemcode | item_abb | qty | price |
+----------+-------------------------+-----+--------+
| 02901 | King Bed | 34 | 150000 |
| 26350 | King Size Dinning Table | 33 | 15000 |
| 33321 | Sofa | 30 | 4500 |
| 4526 | chandelier | 22 | 5000 |
+----------+-------------------------+-----+--------+
4 rows in set (0.00 sec)
mysql> select * from transaction;
+---------------------+-----------+----------+-----------+------+--------------+
| datetime | transtype | itemcode | prevstock | qty1 | currentstock |
+---------------------+-----------+----------+-----------+------+--------------+
| 2017-04-08 20:49:37 | stockin | 4526 | 11 | 12 | 23 |
| 2017-04-08 20:50:35 | stockout | 2901 | 40 | 2 | 38 |
| 2017-04-08 21:00:35 | stockout | 4526 | 23 | 1 | 22 |
| 2017-04-08 22:24:16 | stockout | 2901 | 38 | 2 | 36 |
| 2017-04-09 06:41:47 | stockout | 2901 | 36 | 2 | 34 |
+---------------------+-----------+----------+-----------+------+--------------+
5 rows in set (0.00 sec)
mysql> select * from transaction left join item on transaction.itemcode = item.itemcode;
+---------------------+-----------+----------+-----------+------+--------------+----------+------------+------+-------+
| datetime | transtype | itemcode | prevstock | qty1 | currentstock | itemcode | item_abb | qty | price |
+---------------------+-----------+----------+-----------+------+--------------+----------+------------+------+-------+
| 2017-04-08 20:49:37 | stockin | 4526 | 11 | 12 | 23 | 4526 | chandelier | 22 | 5000 |
| 2017-04-08 20:50:35 | stockout | 2901 | 40 | 2 | 38 | NULL | NULL | NULL | NULL |
| 2017-04-08 21:00:35 | stockout | 4526 | 23 | 1 | 22 | 4526 | chandelier | 22 | 5000 |
| 2017-04-08 22:24:16 | stockout | 2901 | 38 | 2 | 36 | NULL | NULL | NULL | NULL |
| 2017-04-09 06:41:47 | stockout | 2901 | 36 | 2 | 34 | NULL | NULL | NULL | NULL |
+---------------------+-----------+----------+-----------+------+--------------+----------+------------+------+-------+
5 rows in set (0.00 sec)
Upvotes: 1
Views: 32
Reputation: 1951
When you use a left join all rows from the table on the left side of the join are returned. It looks like you were intending to use an inner join where only rows where both tables have the join condition exist.
select * from transaction join item on transaction.itemcode = item.itemcode;
Upvotes: 1