mysql - 缺少结尾,无法执行mysql交叉连接

标签 mysql join

我正在尝试在 mysql 中的两个 Select 语句之间进行交叉连接。 但是,我收到一条错误消息,提示缺少结尾。 但是,那里不需要结束语句

这是代码

SELECT starts_at, (@row := @row + 1) + start_health_post_id as health_post_id
FROM
(
    SELECT DATE_ADD(start_date, INTERVAL start_time MINUTE) AS starts_at
    FROM days
    CROSS JOIN times
) 
slots, (SELECT @row := -1)r
ORDER BY starts_at;

cross join 

Select practices.id as practice_id, providers.id as provider_id, 
        practice_locations.id as practice_location_id
from practices 
Inner Join  providers on practices.health_post_id = providers.health_post_id

Inner join practice_locations on practices.health_post_id =     

practice_locations.health_post_id;

最佳答案

根据MySQL手册:

In MySQL, JOIN, CROSS JOIN, and INNER JOIN are syntactic equivalents (they can replace each other). In standard SQL, they are not equivalent. INNER JOIN is used with an ON clause, CROSS JOIN is used otherwise.

我认为您需要将两个查询括在 ( ) 中:

SELECT t1.*, t2.*
FROM
  (
    SELECT starts_at, (@row := @row + 1) + start_health_post_id as health_post_id
    FROM
    (
        SELECT DATE_ADD(start_date, INTERVAL start_time MINUTE) AS starts_at
        FROM days
        CROSS JOIN times
    ) 
    slots, (SELECT @row := -1) r
    ORDER BY starts_at
  ) as t1
  CROSS JOIN
  (
    Select practices.id as practice_id, providers.id as provider_id, 
            practice_locations.id as practice_location_id
    from practices 
    Inner Join  providers on practices.health_post_id = providers.health_post_id

    Inner join practice_locations on practices.health_post_id =     

    practice_locations.health_post_id
  ) as t2
  ON t1.`t1_field` =  t2.`t2_field` -- change it as you need;

关于mysql - 缺少结尾,无法执行mysql交叉连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37994123/

相关文章:

sql - JOIN 还是子查询更快?

java - 如何修复由于时区更改而导致的 MySQL 错误?

PHP 日期获取微秒精度以匹配 MySQL 列数据类型

php - 从数据库中查找上周报告

c# - 获取导致异常的查询

mysql - 查询以显示所有表及其排序规则

mysql - Conditional INNER JOIN or LEFT JOIN 基于连接条件

php - codeigniter加入2表数据

mysql - 左连接表本身 sql

对 JOINed 表中的列进行 SUM 的 SQL 查询