简体   繁体   English

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

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

This is my original code and i get only results for the title. 这是我的原始代码,我只得到标题的结果。

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

I want get results and from another columns and fields like 我想要从其他列和字段中获取结果

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

I'm guessing you're trying to select a single column from 3 different tables at the same time. 我猜您正在尝试同时从3个不同的表中选择一个列。 If that is the case, you want to use a Union . 在这种情况下,您想使用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"

It's important to note, you won't be able to tell which rows came from which tables. 请务必注意,您将无法分辨哪些行来自哪些表。 You'll need to add a controlling column to identify, like so: 您需要添加一个控制列以进行标识,如下所示:

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