搜索Mysql数据库的多个表
我有以下代码:
$query = "select * from customer where Surname like \"%$trimmed%\" OR TitleName like \"%$trimmed%\" OR PostCode like \"%$trimmed%\"
order by Surname";
但是,我有另一个表,我想使用与该表相同的参数(变量)进行搜索。我知道像“select * from customer,othertable
”之类的东西可能是不可能的,有没有办法做到这一点?
通过使用 UNION 它会切断我的页面,甚至无法正确搜索它
$query = "select * from customer where Surname like \"%$trimmed%\" OR TitleName like \"%$trimmed%\" OR PostCode like \"%$trimmed%\"
order by Surname UNION select * from mooring where Number like \"%$trimmed%\"";
I have the following code:
$query = "select * from customer where Surname like \"%$trimmed%\" OR TitleName like \"%$trimmed%\" OR PostCode like \"%$trimmed%\"
order by Surname";
However, I have another table which I want to search from with the same paramaters(variables) as that. I know that something like "select * from customer,othertable
" might not be possible, Is there a way to do it?
By using UNION it cuts my page off, and doesnt even search it properly
$query = "select * from customer where Surname like \"%$trimmed%\" OR TitleName like \"%$trimmed%\" OR PostCode like \"%$trimmed%\"
order by Surname UNION select * from mooring where Number like \"%$trimmed%\"";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
UNION
运算符从多个表中进行搜索。You can use the
UNION
operator to search from multiple tables.您的 UNION 语法不正确。
Your UNION syntax is incorrect.
如果您的表结构不同,您将必须使用多个查询和多个代码片段来处理不同类型的结果。或者,您可以使用 UNION 运算符从两个表中仅选择相似的字段。您可以将不匹配的字段清空。考虑一下:
If your table structures are different you will have to use multiple queries and multiple fragments of code to handle the different type of results. Alternately, you can select only the similar fields from the two tables using the UNION operator. You can null out the fields that do not match up. Consider this: