简体   繁体   中英

MySQL in one table but not in other table with specific ID - query gives too many results?

I have two tables: uri and pages . The uri contains the url of the page and the url of the page is also stored as slug in pages .

Because I can have forwarders that are not connected to the page but are stored in the uri table, I need a query to extract these 'not-connected-to-a-page' url's from the database.

I tried using the following query for that:

SELECT uri.* 
FROM uri
LEFT JOIN pages 
ON uri.f_site_id = pages.f_site_id
WHERE uri.f_site_id = '2' // <-- Site id here
AND pages.slug IS NULL

The problem is that there must be like 30 results, and there are 2112 results... On which every results returns a lot of times! It does a lot more than I am expecting, what is wrong?

If you want the ones that are not connect to pages, then you need to express this in the query:

SELECT uri.* 
FROM uri LEFT JOIN
     pages 
     ON uri.f_site_id = pages.f_site_id
WHERE uri.f_site_id = '2' AND // <-- Site id here
      pages.f_site_id IS NULL;

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