mysql连接引用外部查询表的多个表
因此,这个查询有点复杂,因为我想要的是复杂的,所以主查询生成一个日期列表,我需要在每行空格和MINPRICE追加两个内容:mysql连接引用外部查询表的多个表,mysql,sql,join,left-join,Mysql,Sql,Join,Left Join,因此,这个查询有点复杂,因为我想要的是复杂的,所以主查询生成一个日期列表,我需要在每行空格和MINPRICE追加两个内容: 根据其他表获取每个日期的总空格数 根据其他表获取最低价格 现在它变得更复杂了,因为有一个名为composition的列,它有两个id,并且用逗号分隔,它所做的是检查组合是否存在,搜索这两个id之间可用的较低空间,并替换默认的空格值。但只有当greengo\u availability\u dates\u cabins.spaces不存在时,才会发生这种情况 此外,您应该注意
composition
的列,它有两个id,并且用逗号分隔,它所做的是检查组合是否存在,搜索这两个id之间可用的较低空间,并替换默认的空格值。但只有当greengo\u availability\u dates\u cabins.spaces
不存在时,才会发生这种情况
此外,您应该注意到,在某些情况下,admin\u cruises\u meta
会有重复的CAB行,这是因为这是基于月份和年份,所以查询检查greengo\u可用性日期。date\u start
和admin\u cruises\u meta\u months.month
以匹配月份并显示正确的行
现在我有3个问题:
| id | id_availability | date_starts | date_ends | status | id | id_post | id_cruise | id_program | id_rate | start_date | end_date | combination | status | id_cruise | id_post | id_operator | name | commission_op | profit_ggt | status | cruiseID | rateID | monthID | dateID | spaces | minPrice |
|----|-----------------|-------------|------------|--------|----|---------|-----------|------------|---------|------------|------------|-------------|--------|-----------|---------|-------------|-------|---------------|------------|--------|----------|--------|---------|--------|--------|----------|
| 6 | 3 | 2021-04-01 | 2021-04-11 | 1 | 3 | 462 | (null) | 9 | 9 | 2020-02-06 | 2022-01-09 | 1,6 | 1 | 1 | 462 | 23 | Yacht | 0 | 15 | 1 | 1 | 9 | 3 | 6 | 14 | 2846.25 |
| 5 | 3 | 2021-03-18 | 2021-03-28 | 1 | 3 | 462 | (null) | 9 | 9 | 2020-02-06 | 2022-01-09 | 1,6 | 1 | 1 | 462 | 23 | Yacht | 0 | 15 | 1 | (null) | (null) | (null) | (null) | (null) | (null) |
我想要的结果是:
| id | id_availability | date_starts | date_ends | status | id | id_post | id_cruise | id_program | id_rate | start_date | end_date | combination | status | id_cruise | id_post | id_operator | name | commission_op | profit_ggt | status | cruiseID | rateID | monthID | dateID | spaces | minPrice |
|----|-----------------|-------------|------------|--------|----|---------|-----------|------------|---------|------------|------------|-------------|--------|-----------|---------|-------------|-------|---------------|------------|--------|----------|--------|---------|--------|--------|----------|
| 6 | 3 | 2021-04-01 | 2021-04-11 | 1 | 3 | 462 | (null) | 9 | 9 | 2020-02-06 | 2022-01-09 | 1,6 | 1 | 1 | 462 | 23 | Yacht | 0 | 15 | 1 | 1 | 9 | 3 | 6 | 14 | 2846.25 |
| 5 | 3 | 2021-03-18 | 2021-03-28 | 1 | 3 | 462 | (null) | 9 | 9 | 2020-02-06 | 2022-01-09 | 1,6 | 1 | 1 | 462 | 23 | Yacht | 0 | 15 | 1 | 1 | 9 | 0 | (null) | 16 | 2984.25 |
查询2的结果如下:
| cruiseID | rateID | monthID | dateID | spaces | minPrice |
|----------|--------|---------|--------|--------|----------|
| 1 | 9 | 3 | 6 | 14 | 2846.25 |
如果在查询#2中应用了查询#3,则最终结果应该是这样,请注意,第二行显示的是2个空格,因为不存在自定义值,并且存在组合
,因此它会得到ID 1和6之间的最小空格值:
| id | id_availability | date_starts | date_ends | status | id | id_post | id_cruise | id_program | id_rate | start_date | end_date | combination | status | id_cruise | id_post | id_operator | name | commission_op | profit_ggt | status | cruiseID | rateID | monthID | dateID | spaces | minPrice |
|----|-----------------|-------------|------------|--------|----|---------|-----------|------------|---------|------------|------------|-------------|--------|-----------|---------|-------------|-------|---------------|------------|--------|----------|--------|---------|--------|--------|----------|
| 6 | 3 | 2021-04-01 | 2021-04-11 | 1 | 3 | 462 | (null) | 9 | 9 | 2020-02-06 | 2022-01-09 | 1,6 | 1 | 1 | 462 | 23 | Yacht | 0 | 15 | 1 | 1 | 9 | 3 | 6 | 14 | 2846.25 |
| 5 | 3 | 2021-03-18 | 2021-03-28 | 1 | 3 | 462 | (null) | 9 | 9 | 2020-02-06 | 2022-01-09 | 1,6 | 1 | 1 | 462 | 23 | Yacht | 0 | 15 | 1 | 1 | 9 | 0 | (null) | 2 | 2984.25 |
< P>查询的任何优化都是因为我不是MySQL的专家而被赏识的。听起来你应该使用CTE(或者如果你不使用MySQL 8。x)在这里发布你的代码,而不仅仅是一个SqLoFIDLE链接。代码太长,不能在这里发布,这就是为什么我使用了SqLFIDDLE,如果代码太长而不能张贴,请考虑缩短它。
| cruiseID | rateID | monthID | dateID | spaces | minPrice |
|----------|--------|---------|--------|--------|----------|
| 1 | 9 | 0 | (null) | 16 | 2984.25 |
| id | id_availability | date_starts | date_ends | status | id | id_post | id_cruise | id_program | id_rate | start_date | end_date | combination | status | id_cruise | id_post | id_operator | name | commission_op | profit_ggt | status | cruiseID | rateID | monthID | dateID | spaces | minPrice |
|----|-----------------|-------------|------------|--------|----|---------|-----------|------------|---------|------------|------------|-------------|--------|-----------|---------|-------------|-------|---------------|------------|--------|----------|--------|---------|--------|--------|----------|
| 6 | 3 | 2021-04-01 | 2021-04-11 | 1 | 3 | 462 | (null) | 9 | 9 | 2020-02-06 | 2022-01-09 | 1,6 | 1 | 1 | 462 | 23 | Yacht | 0 | 15 | 1 | 1 | 9 | 3 | 6 | 14 | 2846.25 |
| 5 | 3 | 2021-03-18 | 2021-03-28 | 1 | 3 | 462 | (null) | 9 | 9 | 2020-02-06 | 2022-01-09 | 1,6 | 1 | 1 | 462 | 23 | Yacht | 0 | 15 | 1 | 1 | 9 | 0 | (null) | 2 | 2984.25 |