sql group\u by和group\u concat如果组中存在所有值

mqkwyuun  于 2021-08-13  发布在  Java
关注(0)|答案(2)|浏览(327)
  1. /*
  2. cars cars_location
  3. +------+---------+ +-------------+-----+ garages
  4. | id | int |<---| car_id | int | +-------+---------+
  5. | name | varchar | | garage_id | int | --->| id | int |
  6. +------+---------+ +-------------+-----+ | name | varchar |
  7. +-------+---------+
  8. * /
  9. drop table if exists cars, garages, cars_location;
  10. create table cars
  11. (
  12. id int not null,
  13. name varchar(255),
  14. primary key (id)
  15. );
  16. create table garages
  17. (
  18. id int not null,
  19. name varchar(255),
  20. primary key (id)
  21. );
  22. create table cars_location
  23. (
  24. car_id int not null,
  25. garage_id int not null,
  26. primary key (car_id, garage_id)
  27. );
  28. insert into cars (id, name)
  29. values (1, 'BMW'),
  30. (2, 'Audi'),
  31. (3, 'Volkswagen');
  32. insert into garages (id, name)
  33. values (1, 'Garage1'),
  34. (2, 'Garage2'),
  35. (3, 'Garage3'),
  36. (4, 'Garage4');
  37. insert into cars_location (car_id, garage_id)
  38. values (1, 1),
  39. (1, 2),
  40. (1, 3),
  41. (2, 1),
  42. (2, 2),
  43. (2, 3),
  44. (2, 4),
  45. (3, 1),
  46. (3, 2),
  47. (3, 4);
  48. -- Find all cars placed in all requested garages at the same time.
  49. -- Example below:
  50. /*
  51. ---------------------------------------------------------
  52. cars | garages
  53. ---------------------------------------------------------
  54. BMW | Garage1, Garage2, Garage3 - RETURNED!!
  55. Audi | Garage1, Garage2, Garage3, Garage4 - RETURNED!!
  56. Volkswagen | Garage1, Garage2, Garage4 - NOT returned
  57. ---------------------------------------------------------
  58. * /
  59. select cars.name as cars,
  60. group_concat(
  61. garages.name
  62. ) as places
  63. from cars_location
  64. left join cars on cars.id = cars_location.car_id
  65. left join garages on garages.id = cars_location.garage_id
  66. -- where places in (1,2,3) # problem here because Volkswagen doesnt exist in garage 3 so should not be visible
  67. group by 1

找到同时放置在所有请求的车库中的所有汽车。所以我想实现这个目标

  1. ---------------------------------------------------------
  2. cars | garages
  3. ---------------------------------------------------------
  4. BMW | Garage1, Garage2, Garage3 - RETURNED!!
  5. Audi | Garage1, Garage2, Garage3, Garage4 - RETURNED!!
  6. Volkswagen | Garage1, Garage2, Garage4 - NOT returned because garage3 not present
  7. ---------------------------------------------------------

我不知道如何只构建一个sql查询

  1. select cars.name as cars,
  2. group_concat(
  3. garages.name
  4. ) as places
  5. from cars_location
  6. left join cars on cars.id = cars_location.car_id
  7. left join garages on garages.id = cars_location.garage_id
  8. -- where places MUST BE in (1,2,3) # problem here because Volkswagen doesnt exist in garage 3 so should not be returned by sql
  9. group by 1

http://sqlfiddle.com/#!9/eadd7a/1号机组

csbfibhn

csbfibhn1#

你的问题叫做关系分割。假设 cars_location (car_id, garage_id) 是独一无二的:

  1. select cars.name as cars,
  2. group_concat(
  3. garages.name
  4. ) as places
  5. from cars_location -- no Outer Joins needed as you don't want non-matching locations
  6. join cars on cars.id = cars_location.car_id
  7. join garages on garages.id = cars_location.garage_id
  8. where garages.id in (1,2,3)
  9. group by 1
  10. having count(*) = 3 -- exactly three rows
eoigrqb6

eoigrqb62#

你想要一个 having 条款:

  1. having sum(garage.id = 1) > 0 and
  2. sum(garage.id = 2) > 0 and
  3. sum(garage.id = 3) > 0

相关问题