mysql子查询
1 #进阶7:子查询 2 /* 3 含义: 4 出现在其他语句中的select语句,成为子查询或内查询、 5 外部的查询语句,成为主查询或外查询 6 7 分类: 8 按子查询出现的位置:、 9 select后面: 10 仅仅支持标量子查询 11 FROM后面:、 12 支持表子查询 13 where 或having 后面:*** 14 标量子查询(单行)√ 15 列子查询(多行) √ 16 行子查询 17 exists后面(相关子查询): 18 表子查询 19 20 按结果集的行列数不同:标量子查询(结果集只有一行一列) 列子查询(结果集只有一列多行) 21 22 行子查询(结果集可以有一行多列) 23 、 24 表子查询(结果集 一般为多行多列) 25 26 27 */ 28 #一、where或haing后面 29 /*1、标量子查询(单行子查询) 30 2、列子 查询(多行子查询) 31 3、行子查询(多列多行) 32 特点: 33 ①子查询放在小括号内 34 ②子查询一般放在条件的右侧 35 ③标量子查询,一般搭配着单行操作符使用 36 > < >= >= = <> 37 38 列子查询,一般搭配着多行操作符使用 39 in、any/some、all 40 41 ④子查询的执行优先于主查询执行,主查询的条件用到了子查询的结果 42 */ 43 #1、标量子查询 44 #案例1:谁的工资比Abel高? 45 ①查询Abel的工资 SELECT 46 salary 47 FROM 48 employees 49 WHERE 50 last_name = 'Abel' ②查询员工的信息,满足salary > ①的结果 SELECT 51 * 52 FROM 53 employees 54 WHERE 55 salary > ( 56 SELECT 57 salary 58 FROM 59 employees 60 WHERE 61 last_name = 'Abel' 62 ); 63 64 #案例2:题目:返回jb_id与141号员工相间, salary比143号员工多的员工姓名,job_id和工资 65 #①查询141号员工的job_id 66 SELECT 67 job_id 68 FROM 69 employees 70 WHERE 71 employee_id = 141 #②查询143号员工的salary 72 SELECT 73 salary 74 FROM 75 employees 76 WHERE 77 employee_id = 143 #③查询员工的姓名,job_id和工资,要求job_id=①并且salary>② 78 SELECT 79 last_name, 80 job_id, 81 salary 82 FROM 83 employees 84 WHERE 85 job_id = ( 86 SELECT 87 job_id 88 FROM 89 employees 90 WHERE 91 employee_id = 141 92 ) 93 AND salary > ( 94 SELECT 95 salary 96 FROM 97 employees 98 WHERE 99 employee_id = 143 100 ); 101 102 #案例3:返回公司工资最少的员工的last_name,job_id和salary 103 SELECT 104 last_name, 105 job_id, 106 salary 107 FROM 108 employees 109 WHERE 110 salary = ( 111 SELECT 112 min(salary) 113 FROM 114 employees 115 ); 116 117 #案例4查询最低工资大于50号部门最低工资的部门id和其最低工资 118 #查询50号部门的最低工资 119 SELECT 120 MIN(salary) 121 FROM 122 employees 123 WHERE 124 department_id = 50 #查询每个部门的最低工资 125 SELECT 126 MIN(salary), 127 department_id 128 FROM 129 employees 130 GROUP BY 131 department_id #在②基础筛选,满足min(salary)>①的结果 132 SELECT 133 MIN(salary), 134 department_id 135 FROM 136 employees 137 GROUP BY 138 department_id 139 HAVING 140 MIN(salary) > ( 141 SELECT 142 MIN(salary) 143 FROM 144 employees 145 WHERE 146 department_id = 50 147 ); 148 149 #非法使用标量子查询 150 SELECT 151 MIN(salary), 152 department_id 153 FROM 154 employees 155 GROUP BY 156 department_id 157 HAVING 158 MIN(salary) > ( 159 SELECT 160 salary 161 FROM 162 employees 163 WHERE 164 department_id = 250 165 ); 166 167 #2、列子查询(多行子查询) 168 #案例1:返回 location id是1400或1700的部门中的所有员工姓名 169 #①查询location_id是1400或1700的部门编号 170 SELECT DISTINCT 171 department_id 172 FROM 173 departments 174 WHERE 175 location_id IN (1400, 1700) #查询员工姓名,要求部门号是①列表中的某一个 176 SELECT 177 last_name 178 FROM 179 employees 180 WHERE 181 department_id IN ( 182 SELECT DISTINCT 183 department_id 184 FROM 185 departments 186 WHERE 187 location_id IN (1400, 1700) 188 ); 189 190 #案例2:返回其它部门中比job_id为 'IT PROG’部门任一工资低的员工的:工号、姓名、 job_id以及 salary 191 #查询job_id为'IT PROG’部门任一工资 192 SELECT DISTINCT 193 salary 194 FROM 195 employees 196 WHERE 197 job_id = 'IT_PROG' #查询工号、姓名、 job_id以及 salary,salary<①的任意个结果 198 SELECT 199 last_name, 200 employee_id, 201 job_id, 202 salary 203 FROM 204 employees 205 WHERE 206 salary < ANY ( 207 SELECT DISTINCT 208 salary 209 FROM 210 employees 211 WHERE 212 job_id = 'IT_PROG' 213 ) 214 AND job_id <> 'IT_PROG'; 215 216 #案例3:返回其它部门中比job_id为' IT PROG'部门所有工资都低的员工的员工号、姓名、job_id以及sa1ary 217 SELECT 218 last_name, 219 employee_id, 220 job_id, 221 salary 222 FROM 223 employees 224 WHERE 225 salary < ALL ( 226 SELECT DISTINCT 227 salary 228 FROM 229 employees 230 WHERE 231 job_id = 'IT_PROG' 232 ) 233 AND job_id <> 'IT_PROG'; 234 235 #3、行子查询(结果集一行多列或多行多列) 236 #案例:查询员工编号最小并且工资最高的员工信息 237 SELECT 238 * 239 FROM 240 employees 241 WHERE 242 (employee_id, salary) = #①查询最小的员工编号 243 SELECT 244 MIN( 245 employee_i #①查询最小的员工编号 246 SELECT 247 MIN(employee_id) 248 FROM 249 employeesd 250 ) 251 FROM 252 employees; 253 254 #②查询最高工资 255 SELECT 256 MAX(salary) 257 FROM 258 employees #③查询员工信息 259 SELECT 260 * 261 FROM 262 employees 263 WHERE 264 employee_id = ( 265 SELECT 266 MIN(employee_id) 267 FROM 268 employees 269 ) 270 AND salary = #二、select后面 271 /* 272 紧紧支持栍 273 274 #二、select后面 275 /* 276 紧紧支持标量子查询 277 */ 278 #案例1:查询每个部门的员工个数 279 SELECT 280 d.*, ( 281 SELECT 282 COUNT(*) 283 FROM 284 employees e 285 WHERE 286 e.department_id = d.department_id 287 ) 个数 288 FROM 289 departments dȩǏ子查询 */ #案例1:查询每个部门的员工个数 290 SELECT 291 d.*, ( 292 SELECT 293 COUNT(*) 294 FROM 295 employees e 296 WHERE 297 e.department_id = d.department_id 298 ) 个数 299 FROM 300 departments d; 301 302 #案例2:查询员工号=102的部门名 303 SELECT 304 ( 305 SELECT 306 department_name 307 FROM 308 departments d 309 JOIN employees e ON d.department_id = e.department_id 310 WHERE 311 e.employee_id = 102 312 ) 部门名 #三、from后面 313 /* 314 将子查询结果充当一张表,要求必须写别名 315 */ 316 #案例:查询每个部门的平均工资的工资等级 317 #查询每个部门的平均工资 318 SELECT 319 avg(salary), 320 department_id 321 FROM 322 employees 323 GROUP BY 324 department_id; 325 326 #②连接1的结果集合job_grades表,筛选条件平均工资 BETWEEN lowest_sal and highest_sal 327 SELECT 328 ag_dep.*, g.grade_level 329 FROM 330 ( 331 SELECT 332 avg(salary) ag, 333 department_id 334 FROM 335 employees 336 GROUP BY 337 department_id 338 ) ag_dep 339 JOIN job_grades g ON ag_dep.ag BETWEEN lowest_sal 340 AND highest_sal; 341 342 #四、exists后面(相关子查询) 343 /* 344 EXISTS(完整的查询语句) 345 结果: 1或0 346 347 */ 348 SELECT 349 EXISTS ( 350 SELECT 351 employee_id 352 FROM 353 employees 354 WHERE 355 salary = 30000 356 ); 357 358 #案例1:查询有员工的部门名 359 #in 360 SELECT 361 department_name 362 FROM 363 departments d 364 WHERE 365 department_id IN ( 366 SELECT 367 department_id 368 FROM 369 employees e 370 ); 371 372 #exists 373 SELECT 374 department_name 375 FROM 376 departments d 377 WHERE 378 EXISTS ( 379 SELECT 380 * 381 FROM 382 employees e 383 WHERE 384 d.department_id = e.department_id 385 ); 386 387 #案例2:查询没有女朋友的男神信息 388 #in 389 SELECT 390 bo.* 391 FROM 392 boys bo 393 WHERE 394 bo.id NOT IN ( 395 SELECT 396 boyfriend_id 397 FROM 398 beauty 399 ); 400 401 #EXISTS 402 SELECT 403 bo.* 404 FROM 405 boys bo 406 WHERE 407 NOT EXISTS ( 408 SELECT 409 boyfriend_id 410 FROM 411 beauty 412 WHERE 413 bo.id = b.boyfriend_id 414 ); 415 416 #1、查询和zlotkey相同部门的员工姓名和工资 417 #①查询zlotkey的部门 418 SELECT 419 department_id 420 FROM 421 employees 422 WHERE 423 last_name = 'zlotkey' #②查询部门号=①的姓名和工资 424 SELECT 425 last_name, 426 salary 427 FROM 428 employees 429 WHERE 430 department_id = ( 431 SELECT 432 department_id 433 FROM 434 employees 435 WHERE 436 last_name = 'Zlotkey' 437 ); 438 439 #2查询工资比公司平均工资高的员工的员工号,姓名和工资 440 #查询平均工资 441 SELECT 442 avg(salary) 443 FROM 444 employees #查询工资>上面的员工号,姓名和工资 445 SELECT 446 last_name, 447 employee_id, 448 salary 449 FROM 450 employees 451 WHERE 452 salary > ( 453 SELECT 454 avg(salary) 455 FROM 456 employees 457 ); 458 459 #3查询各部门中工资比本部门平均工资高的员工的员工号,姓名和工资 460 #查询各部门的平均工资 461 SELECT 462 avg(salary), 463 department_id 464 FROM 465 employees 466 GROUP BY 467 department_id #②连接上结果集和employees表,进行筛选 468 SELECT 469 employee_id, 470 last_name, 471 salary, 472 e.department_id 473 FROM 474 employees e 475 INNER JOIN ( 476 SELECT 477 avg(salary) ag, 478 department_id 479 FROM 480 employees 481 GROUP BY 482 department_id 483 ) ag_dep ON e.department_id = ag_dep.department_id 484 WHERE 485 salary > ag_dep.ag; 486 487 #查询和姓名中包含字母u的员工在相同部门的员工的员工号和姓名 488 SELECT 489 employee_id, 490 last_name 491 FROM 492 employees e 493 WHERE 494 department_id IN ( 495 SELECT DISTINCT 496 department_id 497 FROM 498 employees 499 WHERE 500 last_name LIKE '%u%' 501 ); 502 503 #5.查询在部门的1ocation_id为1700的部门工作的员工的员工号 504 #①查询location_id 为1700的部门 505 SELECT DISTINCT 506 department_id 507 FROM 508 departments 509 WHERE 510 location_id = 1700; 511 512 #②查询部门号=①中的任意一个的员工号 513 SELECT 514 employee_id 515 FROM 516 employees 517 WHERE 518 department_id = ANY ( 519 SELECT DISTINCT 520 department_id 521 FROM 522 departments 523 WHERE 524 location_id = 1700 525 ); 526 527 #6.查询管理者是King的员工姓名和工资 528 #①查询姓名为King的员工姓名和工资 529 SELECT 530 employee_id 531 FROM 532 employees 533 WHERE 534 last_name = 'K_ing'; 535 536 #②查询那个员工的manager_id=① 537 SELECT 538 last_name, 539 salary 540 FROM 541 employees 542 WHERE 543 manager_id IN ( 544 SELECT 545 employee_id 546 FROM 547 employees 548 WHERE 549 last_name = 'K_ing' 550 ); 551 552 #7.查询工资最高的员工的姓名,要求first_name和1ast_name显示为一列,列名为姓,名 553 #①查询最高工资 554 SELECT 555 MAX(salary) 556 FROM 557 employees #②查询工资=①的姓名 558 SELECT 559 CONCAT(first_name, last_name) "姓,名" 560 FROM 561 employees 562 WHERE 563 salary = ( 564 SELECT 565 MAX(salary) 566 FROM 567 employees 568 );
本文来自博客园,作者:自律即自由-,转载请注明原文链接:https://www.cnblogs.com/deyo/p/13272052.html