搜索Mysql数据库的多个表

发布于 2024-10-10 09:40:35 字数 601 浏览 0 评论 0原文

我有以下代码:

    $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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凉墨 2024-10-17 09:40:35

您可以使用 UNION 运算符从多个表中进行搜索。

You can use the UNION operator to search from multiple tables.

2024-10-17 09:40:35

您的 UNION 语法不正确。

  1. 列数和类型必须相同。如果不是这种情况,请使用两个查询。
  2. ORDER BY 必须位于查询末尾,而不是 UNION 之前。

Your UNION syntax is incorrect.

  1. The number of columns and types must be the same. If this is not the case use two queries.
  2. The ORDER BY must go at the end of your query, not before a UNION.
待天淡蓝洁白时 2024-10-17 09:40:35

如果您的表结构不同,您将必须使用多个查询和多个代码片段来处理不同类型的结果。或者,您可以使用 UNION 运算符从两个表中仅选择相似的字段。您可以将不匹配的字段清空。考虑一下:

SELECT CustomerID, Surname, PostCode, NULL AS Number, 'CUSTOMER' AS Type 
# Suppose that the 'Number' column does not exist in this table
FROM   Customer
WHERE  Surname   LIKE '%$trimmed%'
OR     TitleName LIKE '%$trimmed%'
OR     PostCode  LIKE '%$trimmed%'

UNION

SELECT MooringID,  Surname, NULL,     Number,         'MOORING'
# Suppose that the 'PostCode' column does not exist in this table
FROM   Mooring
WHERE  Number    LIKE '%$trimmed%'

ORDER BY 2 # ORDER BY goes to the very end of the union query in this case

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:

SELECT CustomerID, Surname, PostCode, NULL AS Number, 'CUSTOMER' AS Type 
# Suppose that the 'Number' column does not exist in this table
FROM   Customer
WHERE  Surname   LIKE '%$trimmed%'
OR     TitleName LIKE '%$trimmed%'
OR     PostCode  LIKE '%$trimmed%'

UNION

SELECT MooringID,  Surname, NULL,     Number,         'MOORING'
# Suppose that the 'PostCode' column does not exist in this table
FROM   Mooring
WHERE  Number    LIKE '%$trimmed%'

ORDER BY 2 # ORDER BY goes to the very end of the union query in this case
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文