-- 查詢Score表中的最高分的學生學號和課程號。 SELECT * FROM students s, scores c WHERE s.sno = c.sno AND c.degree = (SELECT MAX(degree) FROM scores);
-- 查詢每一個職位的最高工資以及人數 SELECT job.jname, res.jid, res.salary, COUNT(1) FROM (SELECT * FROM emp, (SELECT MAX(salary) as max, job_id as jid FROM emp GROUP BY jid) as max_table WHERE emp.job_id = max_table.jid AND emp.salary = max_table.max) as res, job WHERE res.jid = job.id GROUP BY jid;
從表中複製一條數據,並插入 insert into student ( Sname,Sage,Ssex ) select Sname,Sage,Ssex from student where Sid =1;
-- 查詢出1990年之後出生的學生,並升序排列, 顯示字段gender,性別爲則顯示爲'male',女則顯示爲'female',非男或女則顯示爲'other' SELECT Sname, Sage, Ssex, (CASE Ssex WHEN '男' THEN 'male' WHEN '女' THEN 'female' ELSE 'other' END ) as gender FROM student WHERE SUBSTRING(Sage, 1, 4) > 1990 ORDER BY Sage;
查詢存在" 01 "課程但可能不存在" 02 "課程的狀況(不存在時顯示爲 null ) select * from (select * from sc where sc.CId = '01') as t1 left join (select * from sc where sc.CId = '02') as t2 on t1.SId = t2.SId;
套路:1.使用條件查詢查出存在01課程的狀況;2.根據需求使用內鏈接或者左鏈接、右鏈接查出須要的結果mysql
SELECT a.table_name 表名, a.table_comment 表說明, b.COLUMN_NAME 字段名, b.column_comment 字段說明, b.column_type 字段類型, b.column_key 約束 FROM information_schema.tables a LEFT JOIN information_schema.COLUMNS b ON a.table_name = b.TABLE_NAME WHERE a.table_schema = 'droi'
select substring_index(substring_index('82,83,84,85,86',',',help_topic_id+1),',',-1) as Id from mysql.help_topic where help_topic_id<(length('82,83,84,85,86')-length(replace('82,83,84,85,86',',',''))+1);