如何编写简单.数据查询以加入两个表



我需要编写简单。data查询以遵循SQL查询,您可以帮助我吗?

SELECT
    Table1.UserID,
    Table1.we, Table1.ba, Table1.re,
    Table1.rtes, Table1.datae, Table1.void,
    Table1.deletee
FROM
    Table1
INNER JOIN 
    Table1 ON UserID.UserID = Table2.UserID  
WHERE
    Table2.clinicId = 11

我尝试以下方式

db.Table1.FindAll()
         .Where(db.Table1.UserID == db.Table2.FindAll(db.Table2.ClinicID = 11).Select(db.Table2.UserID));

但它行不通。我使用MySQL 4.0

db.Table2.FindAllByClinicId(11)
  .Select(
    db.Table2.Table1.UserId,
    db.Table2.Table1.we,
    db.Table2.Table1.ba,
    db.Table2.Table2.re,
    db.Table2.Table1.rtes,
    db.Table2.Table1.datae,
    db.Table2.Table1.void,
    db.Table2.Table1.deletee);

最终将其发送到数据库:

SELECT Table1.UserId
     , Table1.we
     , Table1.ba
     , Table1.re
     , Table1.rtes
     , Table1.datae
     , Table1.void
     , Table1.deletee
FROM Table1
     INNER JOIN Table2 ON Table1.UserId = Table2.UserId
WHERE Table2.ClinicId = 11

您正在与同一表加入。因此,您需要加入两个不同的表或加入同一表格。

SELECT Table1.UserID, Table1.we, Table1.ba, Table1.re, Table1.rtes, Table1.datae,
Table1.void, Table1.deletee FROM Table1 INNER JOIN Table2 ON 
UserID.UserID = Table2.UserID
where Table2.clinicId=11

使用别名作为同一表。

SELECT t1.UserID, t1.we, t1.ba, t1.re, t1.rtes, t1.datae, t1.void, t1.deletee FROM 
Table1 as t1 INNER JOIN Table1 as t2 ON UserID.UserID = t2.UserID
    where t2.clinicId=11

这应该有效,您应该只说" table1 innear join tableoe2"。&&条件table1.userid = table2.userid其中table2.clinicid = 11

  SELECT Table1.UserID, Table1.we, Table1.ba, Table1.re, Table1.rtes, Table1.datae,                
  Table1.void, Table1.deletee  
  FROM Table1
  INNER JOIN Table2 
  ON Table1.UserID = Table2.UserID
  where Table2.clinicId=11

相关内容

  • 没有找到相关文章