简体   繁体   English

仅选择带有链接记录的记录

[英]Select only records with linked records

I am seeking a way in mySQL to select only records that have corresponding records in a linked table. 我正在寻找一种在MySQL中选择仅在链接表中具有相应记录的记录的方法。 I am likely suffering tunnel vision, or otherwise missing something simple. 我可能会遇到隧道视觉问题,或者缺少一些简单的东西。

I have the following query which currently works besides this requirement above: 我有以下查询,除了上述要求外,当前还可以使用:

SELECT P.ID, P.NAME, P.SEO_NAME, CI.City, R.Region, C.Country
FROM PROPERTIES P 
LEFT JOIN Cities CI ON P.CITY_ID = CI.CityId
LEFT JOIN Regions R ON P.REGION_ID = R.RegionID
LEFT JOIN Countries C ON P.COUNTRY_ID = C.CountryId 
WHERE APPROVED = '1' AND REGION_ID = '5400' 
ORDER BY RAND() LIMIT 1;

This is related to a previous question of mine, here: Select rows Having Count in linked table 这与我的上一个问题有关,在这里: 选择链接表中具有计数的行

While the answer in this linked thread worked for me at the time, I now require the additional information in the query above. 虽然此链接线程中的答案当时对我有用,但现在我需要上面查询中的其他信息。 Is there any way I can limit it so that only records with records in the linked table PROPERTY_PHOTOS PP (which links ON P.ID = PP.PROPERTY_ID ) 有什么办法可以限制它,以便仅记录包含链接表PROPERTY_PHOTOS PP中的记录(该链接在ON P.ID = PP.PROPERTY_ID

Thanks in advance for any input. 预先感谢您的任何投入。

Try using INNER JOIN instead of LEFT JOIN. 尝试使用INNER JOIN而不是LEFT JOIN。 According to the SQL specifications for INNER JOIN : 根据INNER JOIN的SQL规范:

The INNER JOIN keyword return rows when there is at least one match in both tables.

For the LEFT JOIN , this becomes: 对于LEFT JOIN ,其变为:

The LEFT JOIN keyword returns all rows from the left table (table_name1), 
even if there are no matches in the right table (table_name2).

An INNER JOIN should do this for you: INNER JOIN应该为您做到这一点:

INNER JOIN PROPERTY_PHOTOS PP ON P.ID = PP.PROPERTY_ID

This will only return records where there is a match in both tables. 这只会返回两个表中都匹配的记录。

SELECT P.ID, P.NAME, P.SEO_NAME, CI.City, R.Region, C.Country
FROM PROPERTIES P 
LEFT JOIN Cities CI ON P.CITY_ID = CI.CityId
LEFT JOIN Regions R ON P.REGION_ID = R.RegionID
LEFT JOIN Countries C ON P.COUNTRY_ID = C.CountryId 
**INNER JOIN PROPERTY_PHOTOS PP ON P.ID = PP.PROPERTY_ID**
WHERE APPROVED = '1' AND REGION_ID = '5400' 
ORDER BY RAND() LIMIT 1;

您将需要一个“ INNER JOIN”。

One more JOIN , but not LEFT JOIN一个,但不LEFT

SELECT ...
FROM PROPERTIES P
...
INNER JOIN PROPERTY_PHOTOS PP ON P.ID = PP.PROPERTY_ID

or just 要不就

SELECT ...
FROM PROPERTIES P
...
JOIN PROPERTY_PHOTOS PP ON P.ID = PP.PROPERTY_ID

because they are the same . 因为他们是一样的

Note: This query doesn't actually include a photos table, but I will assume you are joining it as PP 注意:此查询实际上并不包含photos表,但我假设您将其作为PP加入。

SELECT P.ID, P.NAME, P.SEO_NAME, CI.City, R.Region, C.Country
FROM PROPERTIES P 
LEFT JOIN Cities CI ON P.CITY_ID = CI.CityId
LEFT JOIN Regions R ON P.REGION_ID = R.RegionID
LEFT JOIN Countries C ON P.COUNTRY_ID = C.CountryId 
WHERE APPROVED = '1' AND REGION_ID = '5400' AND PP.PROPERTY_ID IS NOT NULL
ORDER BY RAND() LIMIT 1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM