/ / Porównaj dane 2 tabel Mysql o tej samej strukturze - mysql, porównanie danych

Porównaj 2 dane tabel Mysql o tej samej strukturze - mysql, porównanie danych

Mam 2 tabele city_sessions_1 i city_sessions_2 Struktura obu tabel jest podobna

CREATE TABLE `city_sessions_1` (
`city_id` int(11),
`session_date` date,
`start_time` varchar(12),
`end_time` varchar(12) ,
`attendance` int(11) ,
KEY `city` (`city_id`),
KEY `session_date` (`session_date`)
) ENGINE=MyISAM;

Uwaga: te tabele nie mają żadnego klucza podstawowego, ale mają zdefiniowane indeksy. Obie tabele mają tę samą liczbę wierszy. Oczekuje się jednak, że niektóre dane będą inne.

Jak mogę porównać te 2 tabele?

Odpowiedzi:

1 dla odpowiedzi № 1
-- We start with the rows in city_session_1, and their fit in city_session_2
SELECT
* -- or whatever fields you are interested in
FROM city_sessions_1
LEFT JOIN city_sessions_2 ON city_sessions_1.city_id=city_sessions_2.city_id
WHERE
-- Chose only those differences you are intersted in
city_sessions_1.session_date<>city_session_2.session_date
OR city_sessions_1.start_time<>city_session_2.start_time
OR city_sessions_1.end_time<>city_session_2.end_time
OR city_sessions_1.attendance<>city_session_2.attendance

UNION

-- We need those rows in city_session_2, that have no fit in city_session_1
SELECT
* -- or whatever fields you are interested in
FROM city_sessions_2
LEFT JOIN city_sessions_1 ON city_sessions_1.city_id=city_sessions_2.city_id
WHERE city_sessions_1.city_id IS NULL