在另一个表上使用' where '子句从特定表中选择一个值


Select a value from specific table with `where` clause on another table

如何在单个MySQL查询中做这样的事情?

select `value_a` 
from `table_1` 
where `value_b` = (select `value_b` from `table_2` where `value_c` = `x`);

我使用CodeIgniter,所以我可以使用活动记录

您也可以使用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'

您也可以使用您现有的查询,但是x被反引号包围,而不是单引号:

select `value_a` 
from `table_1` 
where `value_b` = (select `value_b` from `table_2` where `value_c` = 'x);