php - 使用带有 LIKE 子句的引用列

标签 php mysql sql

我有一个 SELECT 查询,其中的子查询别名为 nativePhraseStringforeignPhraseString。我想使用别名与搜索字符串进行比较。

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;

但是,nativePhraseStringforeignPhraseString 都无法识别。我如何在 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/

相关文章:

java - 使用准备好的语句的动态列名 + 包含变量的 sql 查询

sql - 将一组行状态合并为一个摘要状态

php - 从多行中选择某些列

php - 如何从 Symfony 中的 Sentry 中排除异常?

php - Xhtml 而不是 Php?

mysql - Orbeon Forms 4.3 不使用 MySQL

php - 按第一个字符过滤列值?

PHP MYSQL 数据计数从 mysql 检索出错

mysql - 在远程位置保存 SQL 备份/转储

mysql - 标准差 (STDDEV) 是否适合该工作?