我有一个 SELECT
查询,其中的子查询别名为 nativePhraseString
和 foreignPhraseString
。我想使用别名与搜索字符串进行比较。
SELECT item.itemID, item.groupID, item.itemType,
(SELECT GROUP_CONCAT(word.string SEPARATOR " ") FROM phrase, phrase_word, word, item AS subItem
WHERE word.wordID=phrase_word.wordID
AND phrase_word.phraseID=phrase.phraseID
AND phrase.phraseID= subItem.nativePhraseID
AND subItem.itemID=item.itemID
ORDER BY phrase_word.wordIndex) AS nativePhraseString,
(SELECT GROUP_CONCAT(word.string SEPARATOR " ") FROM phrase, phrase_word, word, item AS subItem
WHERE word.wordID=phrase_word.wordID
AND phrase_word.phraseID=phrase.phraseID
AND phrase.phraseID= subItem.foreignPhraseID
AND subItem.itemID=item.itemID
ORDER BY phrase_word.wordIndex) AS foreignPhraseString
FROM item
WHERE item.groupID=:groupID
AND nativePhraseString LIKE :search
OR foreignPhraseString LIKE :search;
但是,nativePhraseString
和 foreignPhraseString
都无法识别。我如何在 LIKE
子句中使用它们?
顺便说一句,我正在使用 PDO 来执行此查询。测试查询的其余部分。
最佳答案
一种方法:
SELECT * FROM
(
SELECT item.itemID, item.groupID, item.itemType,
(SELECT GROUP_CONCAT(word.string SEPARATOR " ")
FROM phrase, phrase_word, word, item AS subItem
WHERE word.wordID=phrase_word.wordID
AND phrase_word.phraseID=phrase.phraseID
AND phrase.phraseID= subItem.nativePhraseID
AND subItem.itemID=item.itemID
ORDER BY phrase_word.wordIndex) AS nativePhraseString,
(SELECT GROUP_CONCAT(word.string SEPARATOR " ") FROM phrase,
phrase_word, word, item AS subItem
WHERE word.wordID=phrase_word.wordID
AND phrase_word.phraseID=phrase.phraseID
AND phrase.phraseID= subItem.foreignPhraseID
AND subItem.itemID=item.itemID
ORDER BY phrase_word.wordIndex) AS foreignPhraseString
FROM item
) xx
WHERE xx.groupID=:groupID
AND (xx.nativePhraseString LIKE :search
OR xx.foreignPhraseString LIKE :search);
我在 WHERE 子句中添加了额外的括号以确保您的操作顺序得到维护。
关于php - 使用带有 LIKE 子句的引用列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21066215/