简体   繁体   English

搜索多个字段MySQL

[英]Search multiple fields MySQL

I have a database like this : 我有这样一个数据库:

ID | Name | Model | Type
1  | Car  | 4     | C
2  | Bar  | 2     | B
3  | Car  | 4     | D
4  | Car  | 3     | D

And a form like this : 和这样的形式:

Name : 
Model : 
Type :

Now, I would like to search only the name, for example "Car" and it returns lines 1, 3, 4. (I left Model and Type empty) 现在,我想只搜索名称,例如“Car”,它返回第1,3,4行。(我将模型和类型留空)

If I search "Car" in Name and 4 in Model, it returns lines 1, 3. (I left Type empty) 如果我在Name中搜索“Car”,在Model中搜索4,则返回第1,3行。(我将Type留空)

And if I search "Car" in Name and "D" in Type, it returns line 3, 4 (I left Model empty) 如果我在Name中搜索“Car”,在Type中搜索“D”,则返回第3,4行(我离开了Model空)

Is it possible to do this in one query ? 是否可以在一个查询中执行此操作?

This is what I had : 这就是我所拥有的:

SELECT *
FROM items
WHERE (:name IS NOT NULL AND name = :name)
AND (:model IS NOT NULL AND model = :model)
AND (:type IS NOT NULL AND type = :type)

But it doesn't work. 但它不起作用。 I would like to fill only 2 on 3 fields and the the "WHERE" adapts and ignore the blank field. 我想在3个字段中仅填充2个,并且“WHERE”适应并忽略空白字段。

EDIT 1 : It is a little hard to explain but I have a form. 编辑1:有点难以解释,但我有一个表格。 I want to have only one required field, the two others are optional but if I also fill the one other or two others fields, they act like a filter. 我想只有一个必填字段,另外两个是可选字段,但如果我还填写另外一个或另外两个字段,它们就像一个过滤器。

So the name field is required (in the form). 因此名称字段是必需的(在表单中)。 If I fill only the name field, it will select only where name = :name. 如果我只填写名称字段,它将只选择name =:name的位置。 If I fill name + model, it will select where name = :name AND model = :model. 如果我填写名称+模型,它将选择name =:name AND model =:model。 and so on... 等等...

Thank you for your help. 谢谢您的帮助。

I'm not sure what you mean by "blank", but assuming you mean NULL , you can do something like this: 我不确定你的意思是“空白”,但假设你的意思是NULL ,你可以这样做:

SELECT *
FROM items
WHERE (:name IS NULL OR name = :name) AND
      (:model IS NULL OR model = :model) AND
      (:type IS NULL OR type = :type);

That problem with this query is that it is very hard for MySQL to use indexes for it, because of the or conditions. 这个查询的问题在于,由于条件or条件,MySQL很难使用索引。 If you have a large amount of data, and want to use indexes, then you should construct the where clauses based on the parameters that actually have data. 如果您有大量数据,并且想要使用索引,那么您应该根据实际拥有数据的参数构造where子句。

Here's an alternative approach using PHP. 这是使用PHP的另一种方法。 You'll need to update the variables. 您需要更新变量。

<?php
$query = 'SELECT *
FROM items
WHERE 1 = 1 ';
//below used for testing can be remove
//$_GET['name'] = 'test';
//$_GET['car'] = 'test2';
//$_GET['type'] = 'test3';
if(!empty($_GET['name'])) {
    $query .= ' and name = ? ';
    $params[] = $_GET['name'];
}
if(!empty($_GET['car'])) {
    $query .= ' and car = ? ';
    $params[] = $_GET['car'];
}
if(!empty($_GET['type'])) {
    $query .= ' and type = ? ';
    $params[] = $_GET['type'];
}
if(!empty($params)) {
    $dbh->prepare($query);
    $sth->execute($params);
    //fetch
} else {
    echo 'Missing Values';
}

The 1=1 is so you can append and search field for each field with a value otherwise you'd need to see if it'd already been set. 1=1是这样你可以为每个字段附加and search field ,否则你需要查看它是否已经设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM