如何简化这个 SQL 语句?
我有两个表,首先,我需要进行一些搜索以获取另一个表的 fk,如下所示...
SELECT `related_table_id`
FROM `table_a`
WHERE `users_id` = '14'
AND `user_data_status` = 'n'
AND `another_table_name` = 'table_b'
然后,我有许多 related_table_id 作为输出...并且我需要搜索内容,就像 %keyword% 。 所以,我需要像这样查询另一个SQL:
SELECT *
FROM table_b
WHERE (`table_b_id`='218' OR `table_b_id`='219' OR `table_b_id`='225')
AND `content` LIKE '%keyword%'
问题是,当我的数据库增长时,table_b可能有很多可以从table_a查询的数据,所以,sql语句会变得非常非常长。我可以将这两个语句合并为一个吗?谢谢/
I have two table, first, I need do some searching to get the fk of another table like this...
SELECT `related_table_id`
FROM `table_a`
WHERE `users_id` = '14'
AND `user_data_status` = 'n'
AND `another_table_name` = 'table_b'
then, I have many related_table_id as an output... and I need to search the content, which is like %keyword%.
So, I need to query another SQL like this:
SELECT *
FROM table_b
WHERE (`table_b_id`='218' OR `table_b_id`='219' OR `table_b_id`='225')
AND `content` LIKE '%keyword%'
The question is, when my db grown, and the table_b, may have a lot of data that can query from table_a, so, the sql statement will become very very long. Can I combine these two statement in one? Thank you/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
INNER JOIN
来“匹配“两个或多个表的行。You can use
INNER JOIN
to "match" the rows of two or more tables.执行嵌套查询:
Do a nested query: