簡體   English   中英

從具有不同和限制的列中選擇多個字段

[英]Select multiple fields from columns with distinct and limit

這是我的原始代碼,我只得到標題的結果。

$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"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM