我有这个数据表:
+-----+-----------+------------------------------------+---------+
| ID | post_type | name | term_id |
+-----+-----------+------------------------------------+---------+
| 278 | supplier | Heating | 15 |
| 282 | supplier | Central Heating | 16 |
| 278 | supplier | Biomass | 17 |
| 278 | supplier | Ground Source Heat Pumps | 18 |
| 278 | supplier | Passive Solar | 19 |
| 282 | supplier | Air Source Heat Pumps | 21 |
| 278 | supplier | Air Conditioning | 22 |
| 278 | supplier | Boilers | 23 |
| 277 | supplier | Lighting | 25 |
| 277 | supplier | LED's | 26 |
| 282 | supplier | Halogen | 28 |
| 277 | supplier | CFL | 29 |
| 282 | supplier | Sustainable Construction Materials | 31 |
| 282 | supplier | Plaster | 33 |
| 282 | supplier | Floors | 37 |
| 282 | supplier | Water | 38 |
| 282 | supplier | Showers & Baths | 43 |
| 278 | supplier | Cooling | 44 |
| 278 | supplier | Refrigeration | 46 |
| 282 | supplier | Passive Design | 47 |
| 278 | supplier | Chillers | 48 |
| 282 | supplier | Renewable Energy | 49 |
| 282 | supplier | Air Source Heat Pumps | 53 |
| 282 | supplier | Biomass Heating | 55 |
| 282 | supplier | Biofuels | 57 |
| 282 | supplier | Insulation | 61 |
| 282 | supplier | Wall | 63 |
| 282 | supplier | Floor | 64 |
| 282 | supplier | Draught Proofing | 65 |
| 282 | supplier | Energy Efficiency | 70 |
| 282 | supplier | Gas Boiler Management Systems | 71 |
| 282 | supplier | Low Energy Lighting | 72 |
| 282 | supplier | Voltage Control | 73 |
| 282 | supplier | Smart Meters | 74 |
| 282 | supplier | Electric Heating | 75 |
+-----+-----------+------------------------------------+---------+
并且需要提取ID
适用于指定name
(通配符字符串)和term_id
(整数)的 s 列表。因此,例如,我将搜索ID
具有 aname LIKE '%Lighting%'
和 a term_id = 26
...应该返回的 s ID 277
。
以下查询有效但不漂亮:
SELECT a.ID, d.name, d.term_id
FROM cn_posts AS a
INNER JOIN cn_postmeta AS b ON a.ID = b.post_id
INNER JOIN cn_term_relationships AS c ON a.ID = c.object_id
INNER JOIN cn_terms AS d ON c.term_taxonomy_id = d.term_id
WHERE a.post_status = 'publish'
AND d.name LIKE '%Lighting%'
AND a.ID IN (
SELECT a.ID
FROM cn_posts AS a
INNER JOIN cn_postmeta AS b ON a.ID = b.post_id
INNER JOIN cn_term_relationships AS c ON a.ID = c.object_id
INNER JOIN cn_terms AS d ON c.term_taxonomy_id = d.term_id
WHERE d.term_id = '26'
)
GROUP BY a.ID
我尝试了以下查询,但它们都没有返回任何结果:
SELECT DISTINCT a.ID, d.name, d.term_id
FROM cn_posts AS a
LEFT JOIN cn_postmeta AS b ON a.ID = b.post_id
LEFT JOIN cn_term_relationships AS c ON a.ID = c.object_id
LEFT JOIN cn_terms AS d ON c.term_taxonomy_id = d.term_id
WHERE a.post_status = 'publish'
AND d.name LIKE '%Lighting%'
AND d.term_id = '26'
SELECT a.ID, d.name, d.term_id, a.post_status
FROM cn_posts AS a
JOIN cn_postmeta AS b ON a.ID = b.post_id
JOIN cn_term_relationships AS c ON a.ID = c.object_id
JOIN cn_terms AS d ON c.term_taxonomy_id = d.term_id
WHERE a.post_status = 'publish'
AND d.name LIKE '%Lighting%'
AND d.term_id = '26'
GROUP BY a.ID
这些都返回帖子:
SELECT a.ID, d.name, d.term_id, a.post_status
FROM cn_posts AS a
JOIN cn_postmeta AS b ON a.ID = b.post_id
JOIN cn_term_relationships AS c ON a.ID = c.object_id
JOIN cn_terms AS d ON c.term_taxonomy_id = d.term_id
WHERE a.post_status = 'publish'
AND d.name LIKE '%Lighting%'
SELECT a.ID, d.name, d.term_id, a.post_status
FROM cn_posts AS a
JOIN cn_postmeta AS b ON a.ID = b.post_id
JOIN cn_term_relationships AS c ON a.ID = c.object_id
JOIN cn_terms AS d ON c.term_taxonomy_id = d.term_id
WHERE a.post_status = 'publish'
AND d.term_id = '26'
但是,当您在查询中同时包含 name 和 term_id 时,它什么也不返回?