[英]Mysql Select by name from different table
I have the following 2 tables inside MySQL: 我在MySQL内有以下2个表:
Table Settings 表格设定
|id |name|clientid|
------------------
| 1 | a | 33 |
------------------
| 2 | b | 34 |
------------------
Table Client 表客户端
|id |clientname |
------------------
| 33 | c |
------------------
| 34 | a |
-----------------
Where I am trying to make a Search query where it will always return the id from the first table. 我在尝试进行搜索查询的地方,它将始终从第一个表返回ID。
Query so far: 到目前为止查询:
$this->conn->prepare("SELECT Settings.id as value,
Settings.name as label
FROM Settings
LEFT JOIN client ON Settings.clientid = client.id
WHERE Settings.name LIKE :keyword
OR Settings.id LIKE :keywordid
OR client.clientname LIKE :keywordclient
LIMIT 10");
$stmt->bindValue(':keyword', "%{$this->keyword}%", PDO::PARAM_STR);
$stmt->bindValue(':keywordid', "%{$this->keyword}%", PDO::PARAM_STR);
$stmt->bindValue(':keywordclient', "%{$this->keyword}%", PDO::PARAM_STR);
So basically the idea is the following If Search for letter a the search will do search inside Settings to see if keywrod is LIKE id, or name, or it will have to search and see inside the client table and see if it is like Clientname. 因此,基本的想法是以下内容:如果搜索字母a,则搜索将在“设置”中进行搜索,以查看keywrod是否为LIKE ID或名称,或者必须在客户端表中进行搜索并查看其是否类似于“客户端名称”。 The result will always have to be the Id from Settings table and than the name or client name as label from ther other two pages depending on the result. 结果始终必须是“设置”表中的“ Id”,而不是其他两个页面中的名称或客户端名称作为标签,具体取决于结果。
And in this case the results have to be two 在这种情况下,结果必须是两个
value: 1 label: a
value: 2 /*from Settings table*/ label: a /*from the client table*/
Any help will be appreciate solving this situation. 任何帮助将是感激解决这种情况。
add an order by ORDER BY Settings.id
or ORDER BY value
(forgot which one lol) 通过ORDER BY Settings.id
或ORDER BY value
添加ORDER BY value
(忘了哪一个笑)
SELECT Settings.id as value,
Settings.name as label
FROM Settings
LEFT JOIN client ON Settings.clientid = client.id
WHERE Settings.name LIKE :keyword
OR Settings.id LIKE :keywordid
OR client.clientname LIKE :keywordclient
ORDER BY Settings.id
LIMIT 10
One way is to utilize UNION
, by breaking the problem into two SELECT
queries. 一种方法是通过将问题分为两个SELECT
查询来利用UNION
。 First query would only search in the Settings table; 第一个查询只能在“设置”表中搜索; while the second one would do the same in the Client table only. 而第二个只在Client表中执行相同的操作。 This approach has an added advantage of converting a less-efficient LEFT JOIN
to INNER JOIN
. 这种方法的另一个优势是将效率较低的LEFT JOIN
转换为INNER JOIN
。
The SQL query will be: SQL查询将是:
(SELECT id AS value,
name AS label
FROM settings
WHERE name LIKE :keyword
OR id LIKE :keyword
LIMIT 10)
UNION
(SELECT s.id AS value,
c.name AS label
FROM settings AS s
JOIN client AS c
ON c.id = s.clientid
WHERE c.clientname LIKE :keyword
LIMIT 10)
ORDER BY value
LIMIT
10
Note: Since you want only 10 rows, I have put LIMIT 10
inside both the SELECT
queries, to optimize the data being selected for union later. 注意:由于只需要10行,因此我在两个SELECT
查询中都设置了LIMIT 10
,以优化稍后选择的要合并的数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.