在另一个表上使用`where`子句从特定表中选择一个值 [英] Select a value from specific table with `where` clause on another table
本文介绍了在另一个表上使用`where`子句从特定表中选择一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在单个MySQL查询中执行此类操作?
How can I do something like this in a Single MySQL Query?
select `value_a`
from `table_1`
where `value_b` = (select `value_b` from `table_2` where `value_c` = `x`);
我使用CodeIgniter,所以我可以使用活动记录。
I use CodeIgniter so I can use Active Records.
推荐答案
您也可以使用 JOIN
:
select t1.value_a
from table_1 t1
inner join table_2 t2
on t1.value_b = t2.value_b
where t2.value_c = 'x'
您也可以使用现有的查询,但 / code>由反引号包围而不是单引号:
You can also use your existing query, but the x
is surrounded by backticks and not single quotes:
select `value_a`
from `table_1`
where `value_b` = (select `value_b` from `table_2` where `value_c` = 'x);
这篇关于在另一个表上使用`where`子句从特定表中选择一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文