如何在单个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);