如何编写Simple.Data查询来连接两个表?

时间:2014-01-22 05:10:39

标签: sql database simple.data

我需要为以下SQL查询编写Simple.data查询,你可以帮助我吗?

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

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

你正在加入同一张桌子。所以你需要加入两个不同的表或加入同一个表给别名。

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

OR

使用同一个表的别名。

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

答案 2 :(得分:-1)

这应该有用,你应该说“table1 inner join tabloe2”。 &安培;&安培; condition 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