将奇怪的SQL Server JOIN语法转换为MySQL语法

时间:2017-11-15 03:47:01

标签: sql sql-server join

我有一个SQL Server查询,我试图移植到MySQL,但JOIN语法是我以前从未见过的。查询来自旨在测量过程代码使用情况的视图。 JOIN语法刚刚超过T.PatID = P.ID,第三个LEFT OUTER JOIN,以及我们在MySQL中可以使用的等效语法是什么?它根本不喜欢这种JOIN语法(忽略ISNULL和CONVERT SQL Server特定的语法)

SELECT
                T.Code
              , P.LastName
              , P.FirstName
              , T.TranDate
              , CD.DaysUnits
              , T.TranAmt
              , TD.FullName AS Provider
              , ISNULL(TD.ID, ISNULL(AD.ID, PD.ID))                   AS DoctorID
FROM
                dbo.Doctors AS PD
                INNER JOIN
                                dbo.Transactions AS T
                INNER JOIN
                                dbo.Patients AS P
                                ON
                                                T.PatID = P.ID
                                                ON
                                                                PD.ID = P.DoctorID
                                LEFT OUTER JOIN
                                                dbo.Doctors AS TD
                                                ON
                                                                T.DoctorID = TD.ID
                                LEFT OUTER JOIN
                                                dbo.Doctors AS AD
                                LEFT OUTER JOIN
                                                dbo.Appointments
                                                ON
                                                                AD.ID                                                           = dbo.Appointments.DoctorID
                                                                AND CONVERT(varchar(20), dbo.Appointments.ScheduleDateTime, 8) <> '00:00:00'
                                                                ON
                                                                                T.ApptID = dbo.Appointments.ID
                                                LEFT OUTER JOIN
                                                                dbo.ChargeDetails AS CD
                                                                ON
                                                                                T.ID = CD.ChargeTranID
                                WHERE
                                                (
                                                                T.Code IS NOT NULL
                                                )

SHOW CREATE TABLE如下

CREATE TABLE Doctors
             (

                          ID       int(10) NOT NULL PRIMARY KEY
                        , FullName varchar(50) DEFAULT NULL

             )


CREATE TABLE Patients
             (
                          LName                     varchar(50) DEFAULT NULL
                        , FName                     varchar(50) DEFAULT NULL 
                        , ID                        int(10) NOT NULL PRIMARY KEY

             )

CREATE TABLE Transactions
             (
                           TranType           varchar(2) DEFAULT NULL
                        ,  Code               varchar(100) DEFAULT NULL
                        ,  TranSubType        varchar(2) DEFAULT NULL
                        ,  Description         varchar(2000) DEFAULT NULL
                        ,  TranDate           datetime
                        ,  PatID              int(10) DEFAULT NULL
                        ,  ID                 int(10) NOT NULL PRIMARY KEY
                        ,  TranAmt            decimal(19,4) DEFAULT NULL
                        ,  ApptID             int(10) DEFAULT NULL
                        ,  DoctorID          int(10) DEFAULT NULL

             )


CREATE TABLE ChargeDetails
             (

                           DaysUnits          varchar(50) DEFAULT NULL
                        -- DaysUnits is just an int ranging from 1 to 2
                        ,  ChargeTranID       int(10) NOT NULL PRIMARY KEY
             )

CREATE TABLE Appointments
             (

                           DoctorID              int(10) DEFAULT NULL
                        ,  PatientID            int(10) DEFAULT NULL
                        ,  ScheduleDateTime      datetime DEFAULT NULL
                        ,  ID                   int(10) NOT NULL PRIMARY KEY
             )

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

这是一个使用与第一个查询相同结构的类似(和简化)查询。第二个查询移动连接以使事情更容易阅读。

set nocount on; 
use tempdb;
go

declare @doc table (id int not null); 
declare @tran table (id int not null, patid int not null); 
declare @patients table (id int not null, docid int not null);

insert @doc (id) values (1);
insert @patients (id, docid) values (25, 1);
insert @tran (id, patid) values (100, 25)

select * 
from @doc as pd 
inner join @tran as t
inner join @patients as p 
on t.patid = p.id
on pd.id = p.docid;

select * 
from @tran as t 
inner join @patients as p 
on t.patid = p.id
inner join @doc as pd 
on pd.id = p.docid;

其他事情看起来很奇怪。我没有看到加入约会的需要,但我不会花很多时间来弄清楚逻辑和架构。转换用法似乎是一种检查null的坏方法 - 除非有一个特殊的&#34;标志&#34; datetime值,用作null的等效值。同样,您需要了解查询,查询的目标,它所基于的模式以及如何填充表。坦率地说,这段代码引起了对整个系统质量的担忧 。