I think you can translate that to a simple query:
SELECT d.data1, d.name, d.phone, d.address
, count(*) - 2 * count(e.data1)
FROM (
SELECT data1, name, phone, address
FROM dummytable
-- ORDER BY ???
LIMIT 4
) d
LEFT JOIN existingtable e USING (data1)
GROUP BY d.data1, d.name, d.phone, d.address;
The major ingredient is the LEFT [OUTER] JOIN
. Follow the link to the manual.
count(*)
counts all rows from dummytable
.
count(e.data1)
only counts rows from existingtable
where a matching data1
exists (count()
does not count NULL values). I subtract that twice to match your formula.
About ORDER BY
: There is no natural order in a database table. You need to order by something to get predictable results.
If there can be duplicates in existingtable
but you want to count every distinct data1
only once, eliminate dupes before you join or use an EXISTS
semi-join:
SELECT data1, name, phone, address
, count(*) - 2 * count(EXISTS (
SELECT 1 FROM existingtable e
WHERE e.data1 = d.data1) OR NULL)
FROM (
SELECT data1, name, phone, address
FROM dummytable
-- ORDER BY ???
LIMIT 4
) d
GROUP BY data1, name, phone, address;
The last count works because (TRUE OR NULL) IS TRUE
, but (FALSE OR NULL) IS NULL
.