简体   繁体   English

MYSQL联合PHP选择到一个结果列

[英]MYSQL union PHP select into one result column

Ok, there comes a point where staring at SQL is the only option when Googling has you cross eyed. 好的,当您使用Google搜索时,盯着SQL是唯一的选择。 I can't quite get my head around this. 我对此不太了解。 What I'm trying to do, is simply use 1 query to search multiple tables at once and return what it finds. 我正在尝试做的就是简单地使用1个查询一次搜索多个表并返回找到的内容。 I think I need to just do separate queries for this, but wanted to see if there is a better way. 我想我只需要对此做单独的查询,但是想看看是否有更好的方法。

So, for example, there is a categories table, and a users table. 因此,例如,有一个类别表和一个用户表。 If the user searches for "jo", it might find "jobs" in the categories table, but it should also find "joe" in the users table. 如果用户搜索“ jo”,则它可能在类别表中找到“工作”,但也应该在用户表中找到“ joe”。 These are displayed in a <ul><li> html style with fixed lengths. 这些以固定长度的<ul><li> html样式显示。 Is there a way with one query? 有一种查询方法吗? Ideally if "jobs" is found in the category table, that would end the record and the next record would contain "joe" from the users table (provided that the "jo" didn't have multiple categories from the category table. 理想情况下,如果在类别表中找到“工作”,则该记录将结束,下一条记录将包含来自用户表的“ joe”(前提是“ jo”在类别表中没有多个类别)。

I've played around with UNION SELECT , but am not sure if it can perform in this fashion or not. 我玩过UNION SELECT ,但是不确定它是否可以这种方式执行。 I might be able to get a result back from the query, but since the tables have more than 1 field name that is being searched on, I need to be able to return the result based on where it's from. 我也许可以从查询中获得结果,但是由于表中有多个在搜索的字段名,因此我需要能够根据其来源返回结果。

For example, something like: 例如,类似:

SELECT cat_name as result FROM categories WHERE cat_name LIKE '%$name%' 
UNION (SELECT firstname as result, lastname as result, username as result WHERE 
firstname LIKE '%$name%' OR lastname LIKE '%$name%' OR username LIKE '%$name%' LIMIT 10

<?php echo $row['result']; ?>

Do you think it needs multiple queries? 您是否需要多个查询?

I don't see that there is any harm in using multiple queries and using echo to display the results separately as to not confuse (so you know where to results are coming from). 我看不到使用多个查询和使用echo单独显示结果以免造成混淆(因此您知道结果从何而来)没有任何危害。

Something like this wont burden your script in anyway and makes it easier to read. 这样的事情无论如何都不会加重您的脚本,并使它更易于阅读。

<?php
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "SELECT cat_name FROM categories WHERE cat_name LIKE '%$name%';";
$result = mysqli_query($con,$sql);

echo "<h1>Category Matches</h1>"
echo "<ul>";
while($row = mysqli_fetch_array($result)) {
  echo "<li>" . $row['cat_name'] . "</li>";
}
echo "</ul>";

$sql2 = "SELECT * FROM users WHERE first_name LIKE '%$name%';";
$result2 = mysqli_query($con,$sql);

echo "<h1>User Matches</h1>"
echo "<ul>";
while($row = mysqli_fetch_array($result2)) {
  echo "<li>" . $row['first_name'] . "</li>";
}
echo "</ul>";

mysqli_close($con);
?> 

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

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