我正在嘗試使用post_type= "attachment" 回傳這些帖子,其中meta_value(url) 沒有出現在任何post_content.
現在我正在嘗試meta_value手動提供,在這種情況下是影像名稱speed1
我查了一下,有一些帶有影像“speed1.jpg”的帖子post_content。
當我選擇內容中帶有單詞的那些時,它會起作用,當我嘗試使用查詢選擇除內容中帶有單詞的其他所有內容時,就會出現問題:
SELECT
i.ID,
i.post_content,
i.post_type
FROM
wp_posts i
WHERE
i.post_type = 'attachment'
AND NOT EXISTS (SELECT * FROM wp_posts p WHERE p.post_type <> 'attachment' AND p.post_content LIKE "%speed1%")
這總是回傳空。為什么?
uj5u.com熱心網友回復:
這是我用作以下查詢的 wp_posts 的粗略模擬的簡單 CTE -
WITH wp_posts (ID, post_content, post_type, meta_value) AS (
SELECT 1, null, 'attachment', 'speed1' UNION ALL
SELECT 2, null, 'attachment', 'speed2' UNION ALL
SELECT 3, null, 'attachment', 'speed3' UNION ALL
SELECT 4, 'blah blah speed1 blah blah', 'post', null UNION ALL
SELECT 5, 'blah blah speed3 blah blah', 'post', null
)
它看起來像一張桌子 -
| ID | post_content | post_type | 元值 |
|---|---|---|---|
| 1 | 速度1 | 依戀 | 速度1 |
| 2 | 速度2 | 依戀 | 速度2 |
| 3 | 速度3 | 依戀 | 速度3 |
| 4 | 嗚嗚嗚速度1 嗚嗚嗚 | 郵政 | |
| 5 | 哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇 | 郵政 |
您當前的子查詢在NOT EXISTS-
SELECT *
FROM wp_posts p
WHERE p.post_type <> 'attachment'
AND p.post_content LIKE "%speed1%"
總是從我的模擬中回傳 ID 為 4 的行。這意味著NOT EXISTS總是回傳 false,因為有結果。
如果您將子查詢更改為使用外部查詢中的 meta_value,您將獲得更有意義的東西 -
SELECT
i.ID,
i.post_content,
i.post_type
FROM
wp_posts i
WHERE
i.post_type = 'attachment'
AND NOT EXISTS (
SELECT *
FROM wp_posts p
WHERE p.post_type <> 'attachment'
AND p.post_content LIKE CONCAT('%', i.meta_value, '%')
)
這將回傳 ID 為 2 的行,因為“speed2”不在“post”型別的兩個 wp_post 中的任何一個的 post_content 中。我希望你能夠理解這個簡單的演示。
uj5u.com熱心網友回復:
你能做這樣的事情嗎
SELECT
i.ID,
i.post_content,
i.post_type
FROM
wp_posts i
WHERE
i.post_type = 'attachment'
i.post_content NOT LIKE "%speed1%"
uj5u.com熱心網友回復:
文字值必須用單引號括起來,而不是用撇號(雙引號)括起來。SQL 中的物件名(表名、列名...)需要雙引號括起來。
在您的查詢部分:
NOT LIKE "%speed1%"
正在處理名為 %speed1 %的列
這與比較靜態值不同......我認為你會更好:
NOT LIKE '%speed1%'
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/421555.html
標籤:
