我想知道内部连接在 mysql 中究竟是如何工作的。
如果我做
SELECT * FROM A a
INNER JOIN B b ON a.row = b.row
INNER JOIN C c ON c.row2 = b.row2
WHERE name='Paul';
它是否先进行连接,然后选择 name = paul 的连接?因为当我这样做时,它超级慢。
有没有办法做一些事情:
SELECT * FROM (A a WHERE name='paul')
INNER JOIN B b ON a.row = b.row
INNER JOIN C c ON c.row2 = b.row2]
当我以这种方式尝试时,我只是得到一个错误。
或者,最好只有 3 个单独的查询,一个用于 A、B 和 C?例子:
string query1 = "SELECT * FROM A WHERE name = 'paul'";
//send query, get data reader
string query2 = "SELECT * FROM b WHERE b = " + query1.b;
//send query, get data reader
string query3 = "SELECT * FROM C WHERE c = " + query1.c;
//send query, get data reader
显然这只是伪代码,但我认为它说明了这一点。
哪种方式更快/推荐?
编辑 表结构:
**tblTimesheet**
int timesheetID (primary key)
datetime date
varchar username
int projectID
string description
float hours
**tblProjects**
int projectID (primary key)
string project name
int clientID
**tblClients**
int clientID
string clientName
The join that I want is:
select * from tblTimesheet time
INNER JOIN tblProject proj on time.projectID = proj.projectID
INNER JOIN tblClient client on proj.clientID = client.clientID
WHERE username = 'paul';
类似的东西