我有两张 table
table: t1
id|name|type
1 | a |0
2 | b |1
3 | c |0
4 | d |0
...
table: t2
uid|type
4 |1
3 |0
...
我想统计或获取表 t1 中的记录 type = 1 并且表 t2 中的记录 id = uid type = 1
在一个查询中计算或获取记录的最佳方法是什么
$type = 1;
$count = mysql_num_rows(mysql_query("Select * .... Where type = $type ")); //$count = 2
$result = mysql_query("Select * ... order by id desc "); // $result include record in table t1 has id (4, 2) b/c order by id desc
编辑:
我做了一个 sql 来尝试 http://www.sqlfiddle.com/#!2/8847d1/12
但我不能那样做
我希望记录结果看起来像(所有列都有我的上述条件)
id|name|
4 |d |
2 |b |
如何做到这一点谢谢
最佳答案
select t1.id,t1.name from t1 left join t2 on t1.id = t2.uid where t1.type = 1 or t2.type = 1
关于php - 如何在两个表中使用条件在一个表中获取记录 - PHP 和 Mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17961261/