I have a MySQL database with two tables:
- Table 1 (table1_Temp), is a temporary table that has only one column (word1) with these values:
+-------+ | word1 | +-------+ | aaa | | aaa | | bbb | | ccc | | ccc | | ccc | | ddd | +-------+
- Table 2 (table2_stock) has 3 columns with these values:
+-----+-------+-------+ | id | word2 | stock | +-----+-------+-------+ | 001 | aaa | 2 | | 002 | ccc | 5 | | 003 | ccc | 3 | | 004 | ddd | 1 | | ... | ... | ... | +-----+-------+-------+
I have tried with this code::
SELECT
c.id,
c.word2,
c.stock,
count( d.word1 ) AS Count
FROM
table2_stock c
INNER JOIN table1_Temp d ON d.word1 = c.word2
GROUP BY c.id
But the result is:
+-----+-------+-------+-------+ | id | word2 | stock | Count | +-----+-------+-------+-------+ | 001 | aaa | 2 | 2 | | 002 | ccc | 5 | 3 | | 003 | ccc | 3 | 3 | | 004 | ddd | 1 | 1 | +-----+-------+-------+-------+
Count = number of times each value in table 2 appears in table 1.
I would like to get the unique values from table 1, to know which values are duplicated in table 2 and cross the information with table 2 but leaving all the values including those not present in table 2 (e.g. bbb).
This would be my desired result:
+------+-------+-------+--------+-----------+ | id | word2 | stock | Count |*Duplicate*| +------+-------+-------+--------+-----------+ | 001 | aaa | 2 | 2 | 1 | | NULL | bbb | NULL | 1 | 0 | | 002 | ccc | 5 | 3 | 2 | | 003 | ccc | 3 | 3 | 2 | | 004 | ddd | 1 | 1 | 1 | +------+-------+-------+--------+-----------+
Duplicates = number of times each unique value in table 1 appears in table 2.
In this way you would have all the information on both stock and the number of duplicates in a single table.
How could I get to combine both tables and know what the duplicate values are?.
Thank you very much.
Wardiam
duplicate
column? – Mureinik