Saltar al contenido
Codifíca.me | Desarrollo web | Programación

MySql ejemplo de Select Union

27 marzo, 2019
mysql

MySql ejemplo de Select Union entre varias tablas.

SELECT DISTINCT (
pc.post_id
), pc.post_type, t.name , pc.address, pc.latitude, pc.longitude, p.post_title, pm.meta_value
FROM `wp_postcodes` pc, wp_postmeta pm, wp_posts p, wp_term_relationships tr, wp_terms t
WHERE pc.post_id = p.id
AND pc.post_id = pm.post_id
AND pc.post_type = 'empresa'
AND (pm.meta_key = 'phone'
AND NOT pm.meta_key = 'email' )
AND object_id=p.id
AND term_taxonomy_id = t.term_id
AND t.name <>'Empresas y negocios'
AND t.name <>'Destacadas portada'
UNION
SELECT DISTINCT (
pc.post_id
), pc.post_typSELECT DISTINCT (
pc.post_id
), pc.post_type, t.name , pc.address, pc.latitude, pc.longitude, p.post_title, pm.meta_value
FROM `wp_postcodes` pc, wp_postmeta pm, wp_posts p, wp_term_relationships tr, wp_terms t
WHERE pc.post_id = p.id
AND pc.post_id = pm.post_id
AND pc.post_type = 'empresa'
AND (pm.meta_key = 'phone'
AND NOT pm.meta_key = 'email' )
AND object_id=p.id
AND term_taxonomy_id = t.term_id
AND t.name <>'Empresas y negocios'
AND t.name <>'Destacadas portada'
UNION
SELECT DISTINCT (
pc.post_id
), pc.post_type, t.name , pc.address, pc.latitude, pc.longitude, p.post_title, pm.meta_value
FROM `wp_postcodes` pc, wp_postmeta pm, wp_posts p, wp_term_relationships tr, wp_terms t
WHERE pc.post_id = p.id
AND pc.post_id = pm.post_id
AND pc.post_type = 'empresa'
AND (pm.meta_key = 'email'
AND NOT pm.meta_key = 'phone' )
AND object_id=p.id
AND term_taxonomy_id = t.term_id
AND t.name <>'Empresas y negocios'
AND t.name <>'Destacadas portada'
UNION
SELECT DISTINCT (
pc.post_id
), pc.post_type, t.name , pc.address, pc.latitude, pc.longitude, p.post_title, pm.meta_value
FROM `wp_postcodes` pc, wp_postmeta pm, wp_posts p, wp_term_relationships tr, wp_terms t
WHERE pc.post_id = p.id
AND pc.post_id = pm.post_id
AND pc.post_type = 'empresa'
AND (pm.meta_key = 'email'
AND pm.meta_key = 'phone' )
AND object_id=p.id
AND term_taxonomy_id = t.term_id
AND t.name <>'Empresas y negocios'
AND t.name <>'Destacadas portada'

MySql ejemplo de Select Union entre varias tablas.

mysql-encriptar