简体   繁体   中英

What is wrong with my sql query

I am trying to select from two separate tables and also trying to left join a different table by two different ids.

I keep getting this error:

#1066 - Not unique table/alias: 'wp_term_taxonomy'

SELECT  wp_terms.name, 
    wp_terms.slug, 
    wp_ads_categories.seo_description
FROM wp_terms, wp_ads_categories
LEFT JOIN wp_term_taxonomy ON wp_terms.term_id = wp_term_taxonomy.term_id
LEFT JOIN wp_term_taxonomy ON wp_ads_categories.term_id = wp_term_taxonomy.term_id
WHERE wp_term_taxonomy.taxonomy = 'product_cat'

Here's an example of a valid query. It can't be the query you want, because, like your own query, it's basically nonsensical. But we can't know what the query you actually want is at this stage...

SELECT t.name
     , t.slug
     , c.seo_description
  FROM wp_terms t
 CROSS
  JOIN wp_ads_categories c
  LEFT 
  JOIN wp_term_taxonomy x
    ON x.term_id = t.term_id 
  LEFT 
  JOIN wp_term_taxonomy y
    ON y.term_id = c.term_id 
   AND y.taxonomy = 'product_cat';

The query is nonsensical because there is generally no point OUTER JOINing tables from which you select no columns.

You need to create aliases if you intend to use the same table more than once in the same query. Your query should be.

SELECT  wp_terms.name, 
    wp_terms.slug, 
    wp_ads_categories.seo_description
FROM wp_terms, wp_ads_categories
LEFT JOIN wp_term_taxonomy tax1 ON wp_terms.term_id = tax1.term_id
LEFT JOIN wp_term_taxonomy tax2 ON wp_ads_categories.term_id = tax2.term_id
WHERE tax1.taxonomy = 'product_cat'

The where clause might also be like this. It depends on how you want the result set to be.

WHERE tax2.taxonomy = 'product_cat'

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