简体   繁体   中英

Count occurrences of distinct values with multiple tables

I have 3 tables in a database that have similar values and the same table structure. I am trying to get the number of occurrences of each value by unique user .

DB Structure
View on SQLFiddle

TABLE_1

user | value | id 

TABLE_2

user | value | id 

TABLE_3

user | value | id 

I can run the following MySQL command to retrieve the desired results on 1 table at a time.

SELECT value,COUNT(*) as count FROM TABLE_1 GROUP BY value ORDER BY count DESC;

I need to run this command across the three tables at once in order to retrieve the unique occurrences of "value" among a list of "users" that contains numerous duplicates.

Given your comments, since you want to remove duplicates, use UNION to combine the data from the tables together:

SELECT value, COUNT(*) as count 
FROM (
  SELECT user, value, id
  FROM TABLE_1
  UNION
  SELECT user, value, id
  FROM TABLE_2
  UNION
  SELECT user, value, id
  FROM TABLE_3 ) t
GROUP BY value 
ORDER BY count DESC;

You need to use UNION -

SELECT value, COUNT(*) as count 
FROM (
    SELECT user, value, id
    FROM TABLE_1
    UNION
    SELECT user, value, id
    FROM TABLE_2
    UNION
    SELECT user, value, id
    FROM TABLE_3 ) tables
GROUP BY value 
ORDER BY count DESC;

Output -

+-------+-----+
|car    |8    |
|boat   |4    |
|truck  |3    |
|house  |2    |
|skates |1    |
|bike   |1    |
+-------+-----+

to go along with the comments this is what I would recommend you do.

setup:

CREATE TABLE members (
    id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, 
    username varchar(255),
);

INSERT INTO members (username)
SELECT DISTINCT user FROM table1
UNION SELECT DISTINCT user FROM table2
UNION SELECT DISTINCT user FROM table3;

altering:

ALTER table1
    ADD COLUMN user_id INT(10)
    ADD INDEX `user_id` (`user_id`);

ALTER table2
    ADD COLUMN user_id INT(10)
    ADD INDEX `user_id` (`user_id`);

ALTER table3
    ADD COLUMN user_id INT(10)
    ADD INDEX `user_id` (`user_id`);

updating:

UPDATE table1 t,
JOIN members m ON m.username = t.username
    SET t.user_id = m.id;

UPDATE table2 t,
JOIN members m ON m.username = t.username
    SET t.user_id = m.id;

UPDATE table3 t,
JOIN members m ON m.username = t.username
    SET t.user_id = m.id;

removing non normalized data

ALTER table1
    DROP user;

ALTER table2
    DROP user;

ALTER table3
    DROP user;

now you can also set up foreign key contstraints on the user_id and id columns if you would like.

but to query a total count you can just join the tables.. make sure you add an index on each of the id fields so it will join properly.

SELECT your_stuff
FROM members m
LEFT JOIN table1 t1 ON t1.user_id = m.id
LEFT JOIN table2 t2 ON t2.user_id = m.id
LEFT JOIN table3 t3 ON t3.user_id = m.id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM