联表查询
MySQL中不支持 FULL OUTER JOIN. 实现方法
SELECT * FROM t1 LEFT JOIN t2 ON t1.id = t2.id
UNION ALL
SELECT * FROM t1 RIGHT JOIN t2 ON t1.id = t2.id WHERE t1.id IS NULL
联表查询重复字段加表前缀
$alias = 'car';
//$this->_repeating_field = ['user_id','business_code', 'employee_id'];
if (!$field) {
$field = "*";
}
foreach ($param as $k => $li) {
if (in_array($k, $this->_repeating_field)) {
$where[$alias . '.' . $k] = $li;
} elseif ($k) {
$where[$k] = $li;
}
}
联表更新
UPDATE items,month SET items.price='12' WHERE items.id=month.id;
UPDATE a LEFT JOIN b on a.id=b.a_id SET a.title=b.name,a.body=b.body WHERE a.id=1
注意:多表 UPDATE 不可以使用 ORDER BY 或 LIMIT。
联表删除
从数据表t1中把那些id值在数据表t2里有匹配的记录全删除掉
DELETE t1 FROM t1,t2 WHERE t1.id=t2.id
//DELETE FROM t1 USING t1,t2 WHERE t1.id=t2.id
从数据表t1里在数据表t2里没有匹配的记录查找出来并删除掉
DELETE t1 FROM t1 LEFT JOIN T2 ON t1.id=t2.id WHERE t2.id IS NULL
//DELETE FROM t1,USING t1 LEFT JOIN T2 ON t1.id=t2.id WHERE t2.id IS NULL
从两个表中找出相同记录的数据并把两个表中的数据都删除掉
DELETE t1,t2 from t1 LEFT JOIN t2 ON t1.id=t2.id WHERE t1.id=25
注意此处的delete t1,t2 from 中的t1,t2不能是别名