1、统计同一个房屋下的车辆个数:
SELECT houseid,COUNT(*) FROM t_car GROUP BY houseId;
2、在1的基础上,统计拥有最多车辆个数的房间号
SELECT houseId,COUNT(*) as car_num FROM t_car GROUP BY houseId ORDER BY car_num DESC LIMIT 1;
3、用一条sql语句批量新增3条t_car记录
INSERT INTO `t_car` VALUES ('00111', '鄂AB1000', '11111111111111111111111111111121', 'car_brand_01', 'car_color_01', 'car_type_01', '2017-07-23 12:12:00', ''),
('00112', '鄂AC1234', '11111111111111111111111111111122', 'car_brand_02', 'car_color_02', 'car_type_02', '2018-07-12 12:12:00', NULL),
('00113', '鄂AD9999', '11111111111111111111111111111123', 'car_brand_03', 'car_color_00', 'car_type_00', '2018-07-12 12:12:00', NULL);
4、删除缴费时间在2017年1月1日之前的缴费记录
DELETE FROM t_car_pay WHERE DATE(payTime)<'2018-01-01';
5、删除缴费时间在2017年1月1日之前的车辆记录
DELETE FROM t_car where carId in(SELECT carId from t_car_pay where DATE(payTime)<'2018-01-01');
DELETE c.*,p.*
FROM t_car c , t_car_pay p
WHERE (c.carid=p.carid ) AND DATE(p.payTime)<'2018-01-01'; 同时删除多个表
6、根据车牌号用一条sql语句批量修改车辆的图片信息
UPDATE t_car_image set path= 'A-00benz.jpg'
WHERE carid
in (select carid from t_car where carNumber='鄂AL0000' )
7、查询所有车辆最新的缴费记录
select *
from t_car_pay p
where 1>(select COUNT(*) from t_car_pay where carId=p.carid and payTime>p.payTime)
ORDER BY p.carId,p.payTime desc;
参考: