我正在使用php pdo和mysql,我想使用like语句再选择一列。目前我有这个查询。而且此查询工作正常。
$get_name = $db->prepare("SELECT dou.businessName, m.ownerid FROM dineOwnerUser AS dou INNER JOIN menu AS m ON dou.id = m.ownerid WHERE
(m.firstFood LIKE '%$text%'
OR m.secondFood LIKE '%$text%'
OR m.thirdFood LIKE '%$text%'
OR m.fourthFood LIKE '%$text%'
OR m.fifthFood LIKE '%$text%'
OR m.sixthFood LIKE '%$text%'
OR m.seventhFood LIKE '%$text%'
OR m.eightFood LIKE '%$text%'
OR m.ninthFood LIKE '%$text%'
OR m.tenthFood LIKE '%$text%')");
正如您现在所看到的,我在food栏的帮助下仅获得公司名称和所有者ID。现在,我想选择用户输入的确切列,但我不知道如何对确切列进行排序,并从我尝试过的几个列中进行选择。
SELECT dou.businessName , m.ownerid,
(m.firstFood LIKE '%$text%'
OR m.secondFood LIKE '%$text%'
OR m.thirdFood LIKE '%$text%'
OR m.fourthFood LIKE '%$text%'
OR m.fifthFood LIKE '%$text%'
OR m.sixthFood LIKE '%$text%'
OR m.seventhFood LIKE '%$text%'
OR m.eightFood LIKE '%$text%'
OR m.ninthFood LIKE '%$text%'
OR m.tenthFood LIKE '%$text%') FROM dineOwnerUser AS dou INNER JOIN menu AS m ON m.ownerid = dou.id WHERE
(m.firstFood LIKE '%$text%'
OR m.secondFood LIKE '%$text%'
OR m.thirdFood LIKE '%$text%'
OR m.fourthFood LIKE '%$text%'
OR m.fifthFood LIKE '%$text%'
OR m.sixthFood LIKE '%$text%'
OR m.seventhFood LIKE '%$text%'
OR m.eightFood LIKE '%$text%'
OR m.ninthFood LIKE '%$text%'
OR m.tenthFood LIKE '%$text%')
,但未选择必填列。 注意:“%text%”来自表单输入字段,并且一切正常,我只需要正确的mysql查询即可。
编辑:基本上,我想选择确切的食物列,假设用户输入b,而biryani在“ secondFood”列中,所以我想沿着businessName和m.ownerid选择该列。
答案 0 :(得分:0)
您可以使用IF或CASE替换选择,但是查询可能很麻烦
IF (m.firstFood IS NOT NULL, CONCAT('firstFood:', m.firstFood),
IF (m.secondFood IS NOT NULL, CONCAT('secondFood:', m.secondFood),
IF (m.thirdFood IS NOT NULL, CONCAT('thirdFood:', m.thirdFood),
IF (m.fourthFood IS NOT NULL, CONCAT('fourthFood :', m.fourthFood ),
...................................
)))) as thirdColumn
答案 1 :(得分:0)
我认为您想通过单词选择特定的列,例如,用户类型p和pasta在名为m.tenthFood的列中,那么您希望此列的值并将其显示在您的网站上(如果您认为这是不可能的,因为在子查询中无法显示多列(结果),因此您的查询在此处失败。正如您说的那样,使用php pdo表示您正在某处显示此值,因此必须选择所有列,然后执行一些逻辑以删除空列或空列,然后仅显示具有意大利面,pori,panir等的列。为此,您需要此查询。
SELECT dou.businessName , m.ownerid, IF (m.firstFood LIKE '%r%', m.firstFood, "no")
, IF (m.secondFood LIKE '%r%', m.secondFood, "no")
, IF (m.thirdFood LIKE '%r%', m.thirdFood, "no")
, IF (m.fourthFood LIKE '%r%', m.fourthFood, "no")
, IF (m.fifthFood LIKE '%r%', m.fifthFood, "no")
, IF (m.sixthFood LIKE '%r%', m.sixthFood, "no")
, IF (m.seventhFood LIKE '%r%', m.seventhFood, "no")
, IF (m.eightFood LIKE '%r%', m.eightFood, "no")
, IF (m.ninthFood LIKE '%r%', m.ninthFood, "no")
, IF (m.tenthFood LIKE '%r%', m.tenthFood, "no")
FROM dineOwnerUser AS dou INNER JOIN menu AS m ON m.ownerid = dou.id WHERE
(m.firstFood LIKE '%r%'
OR m.secondFood LIKE '%r%'
OR m.thirdFood LIKE '%r%'
OR m.fourthFood LIKE '%r%'
OR m.fifthFood LIKE '%r%'
OR m.sixthFood LIKE '%r%'
OR m.seventhFood LIKE '%r%'
OR m.eightFood LIKE '%r%'
OR m.ninthFood LIKE '%r%'
OR m.tenthFood LIKE '%r%')
说明: AFTER WHERE子句称为子查询,它将仅返回一个答案(以通用语言),因此您要检查哪一列中包含“ P”的内容,然后采用ownerid和企业名称以及所有food列。在包含IF语句的上部,如果不包含它,它将返回colum的值,将返回“ no”。通过使用条件,您可以忽略这些列,而仅显示所需的正确列,例如面食,煎饼..... 希望这对您有用。 干杯