sql语句使用记录
2014-07-01 11:47
369 查看
根据用户链接上数据库命令:mysql -u root -p
然后输入密码
[root@ovp-e5420-002 wuxianji]# mysql -u wuxianji_root -p
Enter password:
查询存在重复的字段(mysql)
select nickname from user where school_id="swiftv" group by nickname having count(*)>1;
SELECT user_id FROM statistics_duration_cu GROUP BY user_id HAVING count(`user_id`) >1;
-----------------------------------------------------------------------------------
按月份统计的查询,每月注册人数:
select DATE_FORMAT(date_added,'%Y-%m') as month,count(*) as shumu from user
where school_id='swiftv' and DATE_FORMAT(date_added,'%Y')=2014 group by month order by month;
---------------------------------------------------------------------------
链接查询:
左连接的两种条件过滤查询方式:
-- 链接查询,后再过滤
SELECT uw.ITEM_USER_ID,uw.ITEM_WORK_ID,wm.* from tlk_uid_wid as uw
LEFT JOIN tlk_workrecordmonth as wm on uw.ITEM_WORK_ID = wm.ITEM_WORK_ID
where uw.ITEM_USER_ID="11e6-1ca7-fa6a5f88-9915-490ea07f921f";
-- 过滤后,在链接查询
SELECT uw.ITEM_USER_ID,uw.ITEM_WORK_ID,wm.* from
(select * from tlk_uid_wid where ITEM_USER_ID="11e6-1ca7-fa6a5f88-9915-490ea07f921f") as uw
LEFT JOIN tlk_workrecordmonth as wm on uw.ITEM_WORK_ID = wm.ITEM_WORK_ID;
左链接:(代码格式分段注意空格),注意条件位置
全表在左,用左链接查询, 如果全表在右侧可以用右链接查询 (user表为全表)
条件与上面语句位置不同的左连接查询,结果与上面不同,但与下面的右连接查询结果相同
右链接:(course为全表)
----------------------------------------------------------
三个表的连接查询:
---------------------------------------------------------------------
以一个表不存在的id为条件去删除另外一个表的数据
delete from A
where id not in (select id from B);
DELETE from question
WHERE course_id not in (select course_id from course);
---------------------------------------------------------------------------------------
三表关联查询,最新回答(答案表-----问题表-----课程表)mysql语句
SELECT an.*, q.questionTitle, c.courseType, c.courseTitle, c.courseSubtitle, c.coverImage
FROM (
SELECT *
FROM answer WHERE school_id="swiftv"
) AS an,
(
SELECT question_id AS questionId, title AS questionTitle
FROM question
WHERE school_id = "swiftv"
) AS q,
(
SELECT course_id AS courseId, course_type AS courseType, title AS courseTitle, subtitle AS courseSubtitle, cover_image AS coverImage
FROM course
WHERE school_id = "swiftv"
) AS c
WHERE an.question_id = q.questionId AND an.course_id = c.courseId
ORDER BY an.date_added DESC
LIMIT 0,5;
-------------------------------------------------------------------------------------------------
查看一天的记录
select * from statistics_duration where date_end BETWEEN '2014-09-18 00:00:00' AND '2014-09-19 00:00:00' GROUP BY user_id;
---------------------------------------------------------------------------------------------------------
查看当天的数据
select * from statistics_duration_cu where to_days(`date`) = to_days(now()) and user_id=#{userId,jdbcType=VARCHAR} and course_id=#{courseId,jdbcType=VARCHAR} and lecture_id=#{lectureId,jdbcType=VARCHAR} and school_id=#{schoolId,jdbcType=VARCHAR};
----------------------------------------------------------------------------------------------------------------
/*root界面,所有时长统计*/
select t1.schoolId,t1.sname,t1.sdomain,t1.dDuration,t1.dAmount,t1.dCount,sum(sd.duration)*60 as sdDuration from
(SELECT dl.school_id AS schoolId,sum(dl.duration) AS dDuration,sum(dl.amount) as dAmount,count(dl.school_id) as dCount,sl.name as sname,sl.domain as sdomain from deposit_log as dl
LEFT JOIN school as sl on sl.school_id=dl.school_id
where dl.status='Y' GROUP BY dl.school_id) as t1 LEFT JOIN statistics_duration_cu as sd on t1.schoolId=sd.school_id GROUP BY t1.schoolId;
-----------------------------------------------------------------------------------------------------------
1。表结构相同的表,且在同一数据库(如,table1,table2)
Sql :insert into table1 select * from table2 (完全复制)
insert into table1 select distinct * from table2(不复制重复纪录)
insert into table1 select top 5 * from table2 (前五条纪录)
2。 不在同一数据库中(如,db1 table1,db2 table2)
sql: insert into db1..table1 select * from db2..table2 (完全复制)
insert into db1..table1 select distinct * from db2table2(不复制重复纪录)
insert into tdb1..able1 select top 5 * from db2table2 (前五条纪录)
------------------------------------------------------------------------------------------------
然后输入密码
[root@ovp-e5420-002 wuxianji]# mysql -u wuxianji_root -p
Enter password:
查询存在重复的字段(mysql)
select nickname from user where school_id="swiftv" group by nickname having count(*)>1;
SELECT user_id FROM statistics_duration_cu GROUP BY user_id HAVING count(`user_id`) >1;
-----------------------------------------------------------------------------------
按月份统计的查询,每月注册人数:
select DATE_FORMAT(date_added,'%Y-%m') as month,count(*) as shumu from user
where school_id='swiftv' and DATE_FORMAT(date_added,'%Y')=2014 group by month order by month;
---------------------------------------------------------------------------
链接查询:
左连接的两种条件过滤查询方式:
-- 链接查询,后再过滤
SELECT uw.ITEM_USER_ID,uw.ITEM_WORK_ID,wm.* from tlk_uid_wid as uw
LEFT JOIN tlk_workrecordmonth as wm on uw.ITEM_WORK_ID = wm.ITEM_WORK_ID
where uw.ITEM_USER_ID="11e6-1ca7-fa6a5f88-9915-490ea07f921f";
-- 过滤后,在链接查询
SELECT uw.ITEM_USER_ID,uw.ITEM_WORK_ID,wm.* from
(select * from tlk_uid_wid where ITEM_USER_ID="11e6-1ca7-fa6a5f88-9915-490ea07f921f") as uw
LEFT JOIN tlk_workrecordmonth as wm on uw.ITEM_WORK_ID = wm.ITEM_WORK_ID;
左链接:(代码格式分段注意空格),注意条件位置
select a.user_id, a.school_id,a.nickname, a.avatar, c.user_id, c.title, c.category_id, c.is_free from user as a LEFT JOIN course as c on a.user_id=c.user_id where a.school_id="test";
全表在左,用左链接查询, 如果全表在右侧可以用右链接查询 (user表为全表)
select a.user_id, a.school_id,a.nickname, a.avatar, c.user_id, c.title, c.category_id, c.is_free from user as a LEFT JOIN course as c on a.user_id=c.user_id and c.is_free="Y" //查询course表的条件 where a.school_id="test";
条件与上面语句位置不同的左连接查询,结果与上面不同,但与下面的右连接查询结果相同
select a.user_id, a.school_id,a.nickname, a.avatar, c.user_id, c.title, c.category_id, c.is_free from user as a LEFT JOIN course as c on a.user_id=c.user_id where a.school_id="test" and c.is_free="Y";
右链接:(course为全表)
select a.user_id, a.school_id,a.nickname, a.avatar, c.user_id, c.title, c.category_id, c.is_free from user as a RIGHT JOIN course as c on a.user_id=c.user_id //链接条件 and c.is_free="Y" //course查询条件 where a.school_id="test";
----------------------------------------------------------
三个表的连接查询:
select a.user_id, is_instructor,a.school_id,a.nickname, a.avatar, c.user_id, c.title, c.category_id, c.is_free, t.json_content, t.course_id from user as a LEFT JOIN course as c on a.user_id=c.user_id LEFT JOIN course_content as t on c.course_id=t.course_id and c.is_free="Y" where a.school_id="test" and a.is_instructor="Y";
---------------------------------------------------------------------
以一个表不存在的id为条件去删除另外一个表的数据
delete from A
where id not in (select id from B);
DELETE from question
WHERE course_id not in (select course_id from course);
---------------------------------------------------------------------------------------
三表关联查询,最新回答(答案表-----问题表-----课程表)mysql语句
SELECT an.*, q.questionTitle, c.courseType, c.courseTitle, c.courseSubtitle, c.coverImage
FROM (
SELECT *
FROM answer WHERE school_id="swiftv"
) AS an,
(
SELECT question_id AS questionId, title AS questionTitle
FROM question
WHERE school_id = "swiftv"
) AS q,
(
SELECT course_id AS courseId, course_type AS courseType, title AS courseTitle, subtitle AS courseSubtitle, cover_image AS coverImage
FROM course
WHERE school_id = "swiftv"
) AS c
WHERE an.question_id = q.questionId AND an.course_id = c.courseId
ORDER BY an.date_added DESC
LIMIT 0,5;
-------------------------------------------------------------------------------------------------
查看一天的记录
select * from statistics_duration where date_end BETWEEN '2014-09-18 00:00:00' AND '2014-09-19 00:00:00' GROUP BY user_id;
---------------------------------------------------------------------------------------------------------
查看当天的数据
select * from statistics_duration_cu where to_days(`date`) = to_days(now()) and user_id=#{userId,jdbcType=VARCHAR} and course_id=#{courseId,jdbcType=VARCHAR} and lecture_id=#{lectureId,jdbcType=VARCHAR} and school_id=#{schoolId,jdbcType=VARCHAR};
----------------------------------------------------------------------------------------------------------------
mysql中把查询出来的结果在进行查询
/*root界面,所有时长统计*/select t1.schoolId,t1.sname,t1.sdomain,t1.dDuration,t1.dAmount,t1.dCount,sum(sd.duration)*60 as sdDuration from
(SELECT dl.school_id AS schoolId,sum(dl.duration) AS dDuration,sum(dl.amount) as dAmount,count(dl.school_id) as dCount,sl.name as sname,sl.domain as sdomain from deposit_log as dl
LEFT JOIN school as sl on sl.school_id=dl.school_id
where dl.status='Y' GROUP BY dl.school_id) as t1 LEFT JOIN statistics_duration_cu as sd on t1.schoolId=sd.school_id GROUP BY t1.schoolId;
-----------------------------------------------------------------------------------------------------------
1。表结构相同的表,且在同一数据库(如,table1,table2)
Sql :insert into table1 select * from table2 (完全复制)
insert into table1 select distinct * from table2(不复制重复纪录)
insert into table1 select top 5 * from table2 (前五条纪录)
2。 不在同一数据库中(如,db1 table1,db2 table2)
sql: insert into db1..table1 select * from db2..table2 (完全复制)
insert into db1..table1 select distinct * from db2table2(不复制重复纪录)
insert into tdb1..able1 select top 5 * from db2table2 (前五条纪录)
------------------------------------------------------------------------------------------------
相关文章推荐
- 转:使用一条SQL语句删除表中重复记录
- 使用一条SQL语句删除表中重复记录
- 使用一条SQL语句删除表中重复记录
- 使用SQL语句逐条更新每条记录
- 使用SQL语句获取数据库中随机N个记录
- 使用SQL语句对重复记录查询、统计重复次数、删除重复数据
- SQL Server中使用SQL语句取各分类中的前几条记录
- SQL语句使用Left Outer join代替In删除多条记录
- hibernate使用hql和sql查询总记录数语句
- 使用一条SQL语句删除表中重复记录
- 使用SQL语句查询表中重复记录并删除
- 使用log4net记录ibatis.net的SQL动态查询语句
- oracle sql 中 like语句使用记录
- 使用SQL语句获取数据库中随机N个记录
- 使用一条SQL语句删除表中重复记录
- SQL Cookbook:一、检索记录(8)在SELECT语句中使用条件逻辑
- 使用一条SQL语句删除表中重复记录
- 使用传入的总记录数实现一条sql语句完成分页查询
- 使用SQL语句对重复记录查询、统计重复次数、删除重复数据
- 如何使用Oracle查询并删除重复记录的SQL语句