I would like to create a view like:
CREATE OR REPLACE VIEW all_cities AS
SELECT city,date_added FROM tableA
UNION DISTINCT
SELECT city,date_added FROM tableB
UNION DISTINCT
SELECT city,date_added FROM tableC;
The all_cities table should only contain unique city entries from other tables however the other tables can have the same cities added with different dates this breaks my union and going to result in having all those duplicates in my all_cities. I still want to see the date_added field from at least one of the merged results (doesn't matter which).
With other words: How can I create a view which creates me an unique list from 3 tables but ignoring the date_added field when it does the uniqueing.
Thanks
The best way for select
select city, min(date_added)
from (
SELECT city,date_added FROM tableA
UNION DISTINCT
SELECT city,date_added FROM tableB
UNION DISTINCT
SELECT city,date_added FROM tableC
)A
GROUP BY city
but MySQL doesn't allow to use sub queries in view statement, however you can use view in view statement, so I would go for this solution with 2 views.
CREATE OR REPLACE VIEW all_cities_support_view AS
SELECT city,date_added FROM tableA
UNION DISTINCT
SELECT city,date_added FROM tableB
UNION DISTINCT
SELECT city,date_added FROM tableC;
CREATE OR REPLACE VIEW all_cities AS
SELECT city, min(date_added) as date_added
FROM all_cities_support_view
GROUP BY city;
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.