简体   繁体   English

如何从2个查询中获得结果

[英]How can I get results from 2 queries

I have a script that searches for users in the db. 我有一个脚本搜索数据库中的用户。 I have two input fields so that I can search for eg city and house number. 我有两个输入字段,以便我可以搜索例如城市和门牌号码。 What I want is that if I type eg London in the city field and the number 20 it returns the record with London AND 20 in it. 我想要的是,如果我在城市字段中输入例如伦敦,而在数字20中输入伦敦,其中包含伦敦和20。 I'm not quite sure how to achieve this. 我不太清楚如何实现这一目标。 I did enough research on Google but still didn't find the answer. 我在Google上做了足够多的研究,但仍未找到答案。

Here is my code: 这是我的代码:

if($_GET['search'] != ''){
$result = mysql_query("SELECT * FROM klant WHERE klant_id LIKE '%" .$search."%' OR             voornaam LIKE '%" .$search."%' OR achternaam LIKE '%" .$search."%' OR email LIKE '%"        .$search."%' OR plaats LIKE '%" .$search."%' OR bedrijfsnaam LIKE '%" .$search."%'ORDER BY       klant_id DESC");


$result2 = mysql_query("SELECT * FROM klant WHERE klant_id LIKE '%" .$search2."%' OR voornaam   LIKE '%" .$search2."%' OR achternaam LIKE '%" .$search2."%' OR email LIKE '%" .$search2."%' OR   plaats LIKE '%" .$search2."%' OR bedrijfsnaam LIKE '%" .$search2."%'ORDER BY klant_id DESC");
$i = 0;

}

What I need is something that will give the proper $result where those two search values ($search and $search2) are combined. 我需要的是能够在这两个搜索值($ search和$ search2)组合的情况下给出正确的$ result的东西。 Thus allowing me to search much easier. 从而让我更容易搜索。

Hope that i phrased my question well enough for you to understand me and I hope that you can help me! 希望我能够很好地表达我的问题,让你了解我,我希望你能帮助我!

好吧,如果你想运行2个查询并得到1个结果集,你应该看一个UNION查询

The words "UNION" (for only distinct values) or "UNION ALL" (to allow repeats) between your two queries will join them into one results table. 两个查询之间的单词“UNION”(仅用于不同的值)或“UNION ALL”(允许重复)将它们连接到一个结果表中。

But when it comes to searches, I highly recommend against using %word%. 但是在搜索方面,我强烈建议不要使用%word%。 It'll slow your search down tremendously once you have a lot of data because SQL will iterate through the length of entire strings to find anything that contains the word in question. 一旦你拥有大量数据,它将极大地降低你的搜索速度,因为SQL会遍历整个字符串的长度,以找到包含所讨论单词的任何内容。

Please try with the following logic - 请尝试以下逻辑 -

if($_GET['search'] != ''){
$search = $_GET['search'];
$where[] = "( klant_id LIKE '%" .$search."%' OR  voornaam LIKE '%" .$search."%' OR achternaam LIKE '%" .$search."%' OR email LIKE '%"        .$search."%' OR plaats LIKE '%" .$search."%' OR bedrijfsnaam LIKE '%" .$search."%' ) ";
}
if($_GET['search2'] != ''){
$search2 = $_GET['search2'];
$where[] = " ( klant_id LIKE '%" .$search2."%' OR voornaam   LIKE '%" .$search2."%' OR achternaam LIKE '%" .$search2."%' OR email LIKE '%" .$search2."%' OR   plaats LIKE '%" .$search2."%' OR bedrijfsnaam LIKE '%" .$search2."%') ";
}
$query_where = (is_array($where))?" WHERE ".implode(" AND ", $where):""; 
$result = mysql_query("SELECT * FROM klant ".$query_where." ORDER BY klant_id DESC");

您可以使用UNION子句例如: First Query .. UNION Second Query .. :)

I believe you could use another OR operator with brackets: 我相信你可以使用带括号的另一个OR运算符:

$result = mysql_query("
    SELECT 
        * 
    FROM 
        klant 
    WHERE 
        (klant_id LIKE '%" .$search."%' OR 
        voornaam LIKE '%" .$search."%' OR 
        achternaam LIKE '%" .$search."%' OR 
        email LIKE '%".$search."%' OR 
        plaats LIKE '%" .$search."%' OR 
        bedrijfsnaam LIKE '%" .$search."%') 
    OR 
        (klant_id LIKE '%" .$search2."%' OR 
        voornaam   LIKE '%" .$search2."%' OR 
        achternaam LIKE '%" .$search2."%' OR 
        email LIKE '%" .$search2."%' OR 
        plaats LIKE '%" .$search2."%' OR 
        bedrijfsnaam LIKE '%" .$search2."%') 
    GROUP BY 
        klant_id
    ORDER BY
        klant_id DESC");

But UNION might look cleaner as the other answers suggest. 但其他答案表明,UNION可能看起来更干净。

Added group by to prevent the same result from displaying multiple times. 添加分组以防止同一结果多次显示。

if($_GET['search'] != ''){

$query1 = "SELECT * FROM klant WHERE klant_id LIKE '%" .$search."%' OR voornaam LIKE '%" .$search."%' OR achternaam LIKE '%" .$search."%' OR email LIKE '%".$search."%' OR plaats LIKE '%" .$search."%' OR bedrijfsnaam LIKE '%" .$search."%'ORDER BY klant_id DESC";

$query2 = "SELECT * FROM klant WHERE klant_id LIKE '%" .$search2."%' OR voornaam   LIKE '%" .$search2."%' OR achternaam LIKE '%" .$search2."%' OR email LIKE '%" .$search2."%' OR plaats LIKE '%" .$search2."%' OR bedrijfsnaam LIKE '%" .$search2."%'ORDER BY klant_id DESC";

$result = mysql_query($query1 . ' UNION ALL ' . $query2;
$i = 0;

}

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

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