# 需要用多少列,就写多少列
select [column1],[column2],......from TableName;
select [column1],[column2] from table1
union all
select [column1],[column2] from table2;
select * from order where userid in (select userid from user where status = 1);
或者
select * from order a where exists (select 1 from user b where a.userid = b.userid);
#最优的sql
select * from user a where exists (select 1 from order b where a.userid = b.userid);
foreach(var item : list)
{
## 添加
}
#对应的sql
insert into order (id,order_name,user)
values(1,'23234','小明');
insert into order (id,order_name,user)
values(1,'23234','小明')(2,'23234','小明2')(3,'23234','小明3');
select id,create_date from order where user_id = 123 order by create_date desc limit 1;
select id,name from user where id ('1','2','3',......,'100');
# 使用limit来闲着查询的数量或者使用分页的方式来控制,这样的性能会达到最佳
select id,name from user where id ('1','2','3',......,'100') limit 500;
select id,name from user where id > #{id} and create_date > #{create_date} limit 100;
# 子查询时通过in关键字实现,一个查询语句中的条件落在另一个select查询结果中,程序先运行在嵌套在最内层的语句,再去运行最外层的查询语句。
select * from user where id in (select user_id from order where status = '1');
# 优点
- 简单、结构化
# 缺点
- 执行子查询时,内部会创建临时表,查询完毕后会删除临时表数据,这样会消耗性能。
select * from user left join order on id = order.user_id and status = '1';
select sex,username from user where age <=18 group by sex,username;
select sex,username from user group by sex,username where age<=18;
加粗样式