mysql联表查询及联表删除
mysql联表查询,需要用到:
select语法
join语法
union语法
例子:
复制代码 代码示例:
mysql> select t1.name, t2.salary from employee as t1, info as t2·-> where t1.name = t2.name;eg2:·mysql> select table1.* from table1·-> left join table2 on table1.id=table2.id·-> where table2.id is null;
mysql联表删除:
1、从数据表t1中把那些id值在数据表t2 里有匹配的记录全删除掉
delete t1 from t1,t2 where t1.id=t2.id 或delete from t1 using t1,t2 where t1.id=t2.id
2、从数据表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
3、从两个表中找出相同记录的数据并把两个表中的数据都删除掉
delete t1,t2 from t1 left join t2 on t1.id=t2.id where t1.id=25
注意此处的delete t1,t2 from 中的t1,t2不能是别名
例子:
delete t1,t2 from table_name as t1 left join table2_name as t2 on t1.id=t2.id where table_name.id=25
在数据里面执行是错误的(mysql 版本不小于5.0在5.0中是可以的)
以上语句,修改为:
delete table_name,table2_name from table_name as t1 left join table2_name as t2 on t1.id=t2.id where table_name.id=25
在数据里中执行是错误的(mysql 版本小于5.0在5.0中是可以的)