sql - 从 2 个表中搜索方法
我有一个网站,搜索功能非常基本,而且总是能完成它需要做的事情。 我现在想将另一个表中的值合并到搜索中,这有点棘手。
我想从一个查询返回一组完整的结果,但新信息仅与我想要返回的另一个表中的数据相关(通过 id)。
我目前
SELECT *
FROM [TABLE]
WHERE ( LOWER(title) LIKE '%$search%'
OR LOWER(contents) LIKE '%$search%'
)
AND type = 'product'
在另一个表中有一个名为 id 的列(与其他相关的相同)和另一个元值。
我不仅想执行上面的操作,而且如果匹配从另一个表(使用 id)获取结果,还可以搜索 meta_value 数据。
如果有人理解我在说什么并且这是可能的,那么如果您能将我推向正确的方向,我将不胜感激。
I have a site up and the search feature was very basic and always has done what it needs to do.
I now want to incorperate a value from another table into the search which gets a bit tricky.
I want to return a complete set of results from one query, but the new info only relates (via id) the the data in the other table which I want to return.
I currently have
SELECT *
FROM [TABLE]
WHERE ( LOWER(title) LIKE '%$search%'
OR LOWER(contents) LIKE '%$search%'
)
AND type = 'product'
in the other table there is a col called id (same as the other which are related) and another meta_value.
I want to not only do the operation above but also search the meta_value data, if a match get the results from the other table (using the id).
If any one understands what I am saying and it is possible then I would be muchly appreciated if you could push me in the right direction.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的问题有点难以理解,但这是我的尝试。您似乎想要加入“其他表”中可能存在或不存在的 ID。只需使用
LEFT JOIN
即可:You're question is a bit tough to understand, but here's my stab at it. It appears that you want to join on an ID that may or may not exist in the "other table". Simply use a
LEFT JOIN
for this:您可以使用 LEFT JOIN 命令通过 ID 来连接两个表。然后,您可以过滤任一表中的列。
You can join the two tables using their IDs using the LEFT JOIN command. You can then filter on columns in either table.
使用上面的两个例子就可以了:
非常感谢大家
This worked using the 2 examples above:
Thank you all very much