gpt4 book ai didi

php - 从具有不同和限制的列中选择多个字段

转载 作者:行者123 更新时间:2023-11-29 23:50:00 25 4
gpt4 key购买 nike

这是我的原始代码,我只得到标题的结果。

$result = $db->fetchRowList("select Distinct `title` from ".TABLE_ADS." where `title` like '$term%' limit 10");

我想从其他列和字段中获取结果,例如

$result = $db->fetchRowList("select Distinct `title` from ".TABLE_ADS." where `title` like '$term%' and ".TABLE_MESSANGES." where `messange` like '$term%'  and ".TABLE_CARS." where `model` like '$term%' limit 10");

最佳答案

我猜您正在尝试同时从 3 个不同的表中选择一列。如果是这种情况,您想使用 Union .

"(SELECT `title` FROM ".TABLE_ADS." WHERE `title` LIKE '$term%') UNION (SELECT `messange` FROM ".TABLE_MESSANGES." WHERE `messange` LIKE '$term%') UNION (SELECT `model` FROM ".TABLE_CARS." WHERE `model` LIKE '$term%') LIMIT 10"

需要注意的是,您将无法分辨哪些行来自哪些表。您需要添加一个控制列来识别,如下所示:

"(SELECT `title`, 'ad' as `table` FROM ".TABLE_ADS." WHERE `title` LIKE '$term%') UNION (SELECT `messange`, 'messange' as `table` FROM ".TABLE_MESSANGES." WHERE `messange` LIKE '$term%') UNION (SELECT `model`, 'car' as `table` FROM ".TABLE_CARS." WHERE `model` LIKE '$term%') LIMIT 10"

关于php - 从具有不同和限制的列中选择多个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25695764/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com