2014-01-22 73 views
0

我需要爲以下SQL查詢編寫Simple.data查詢,你能幫助我嗎?如何編寫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

回答

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 
+0

謝謝。你能告訴我如何選擇*從Ta ble1在你的答案 – user3044294

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 
+0

1用於使用別名,它們來得心應手,當你有很多表 – zee

+0

我不需要sql quries我需要Simple.Data quries – user3044294

-1

這應該工作,你應該只是說「table1 inner join tabloe2」。 & &條件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 
+0

我不需要sql quries我需要Simple.Data quries – user3044294