简体   繁体   English

分组还是加入搜索查询?

[英]Grouping or Joining Search Query?

I'm trying to have my websites search box be able to search multiple tables in one query. 我正在尝试让我的网站搜索框能够在一个查询中搜索多个表。

So if a user searches for the users display_name which is stored in ptb_profiles they get any users with the matching name or if they search by nationality which is stored in ptb_stats they get all the users with that nationality displayed. 因此,如果用户搜索存储在ptb_profiles的用户display_name ,他们将获得具有匹配名称的任何用户,或者如果他们按存储在ptb_stats的国籍进行搜索, ptb_stats显示所有具有该国籍的用户。

When i did this originally by using SELECT * FROM ptb_users, ptb_stats, ptb_profiles I got duplicate search results and the same results were being displayed multiple times, so I tried to do avoid this and have each result only displayed once by Grouping and using union all but that didnt work. 当我最初使用SELECT * FROM ptb_users, ptb_stats, ptb_profiles我得到了重复的搜索结果,并且多次显示相同的结果,因此我尝试避免这种情况,并且仅通过分组并使用union all将每个结果仅显示一次但这没用。

now ive tried this: 现在我尝试了这个:

$query_for_result=mysql_query("SELECT display_name, location, gender, contact_number FROM ptb_profiles WHERE display_name LIKE '%".$query."%' OR location LIKE '%".$query."%' OR gender LIKE '%".$query."%' OR contact_number LIKE '%".$query."%' JOIN SELECT email, subscription FROM ptb_users WHERE email like '%".$query."%' OR subscription like '%".$query."%' JOIN SELECT nationality, hobbies, local_station FROM ptb_stats WHERE nationality like '%".$query."%' OR hobbies like '%".$query."%' OR local_station like '%".$query."%' 
        LIMIT 5");

but this just brings up the following error: 但这只会引发以下错误:

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/PTB1/includes/mod_sidebar/search.php on line 31

Can someone please tell me where I'm going wrong? 有人可以告诉我我要去哪里了吗?

This wouldn't be the only way to do it. 这不是唯一的方法。 But you can do your search on multiple joined tables and fetch a distinct list of users easily if you apply a GROUP BY to your query. 但是,如果将GROUP BY应用于查询,则可以在多个联接的表上进行搜索,并轻松获取不同的用户列表。 Assuming all your tables relate via an ID column something like this might work... 假设您所有的表都通过一个ID列关联起来,则可能会起作用……

SELECT u.* FROM ptb_users u
INNER JOIN ptb_stats s ON s.user_id=u.id
INNER JOIN ptb_profiles p ON p.user_id=u.id
WHERE "this or that"
GROUP BY u.id

Change your joins to LEFT JOINS if its possible a user doesn't have a record in ptb_stats or ptb_profiles... 如果可能的话,用户在ptb_stats或ptb_profiles中没有记录,则将您的联接更改为LEFT JOINS ...

mysql_fetch_array() complains about $query_for_result not being a query result. mysql_fetch_array()抱怨$query_for_result不是查询结果。 You must check the return value from mysql_query() . 您必须检查mysql_query()的返回值。 It returns false, if there's some problem with your SQL statement. 如果您的SQL语句有问题,则返回false。 So do at least 至少也是如此

$sql = "select ...";
$result = mysql_query($sql) or die("Error in $sql: " . mysql_error());

to prevent and diagnose such errors. 预防和诊断此类错误。

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

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