我通常会这样做:
$query = "SELECT table1.ColA, table2.ColC ".
"FROM table1, table2".
"WHERE table1.uid = table2.uid";
但是在这种情况下,WHERE部分已经用于使用PHP变量指定记录。在这种情况下如何加入表2?
SELECT SQL_CALC_FOUND_ROWS ColA, ColB FROM Table1 WHERE value = $value
答案 0 :(得分:4)
他们已加入。它与
相同SELECT table1.ColA, table2.ColC
FROM table1
INNER JOIN table2 ON table1.uid = table2.uid
如果您需要添加更多条件,请将它们添加到WHERE
子句:
SELECT table1.ColA, table2.ColC
FROM table1, table2
WHERE table1.uid = table2.uid
AND value = $value;
或者,使用ANSI SQL-92语法:
SELECT table1.ColA, table2.ColC
FROM table1
INNER JOIN table2 ON table1.uid = table2.uid
WHERE value = $value;
请注意:您应始终使用后面的语法,JOIN
加入WHERE
子句的实例表。这是推荐的方法。有关详细信息,请参阅this post。
答案 1 :(得分:3)
请试试这个
$query = "SELECT table1.ColA, table2.ColC FROM table1,
table2 WHERE table1.uid = table2.uid and table1.value=$value";
OR
$query = "SELECT table1.ColA, table2.ColC FROM table1 INNER JOIN table2
ON (table1.uid = table2.uid) WHERE table1.value=$value";
答案 2 :(得分:1)
试试这个::
SELECT
SQL_CALC_FOUND_ROWS ColA,
ColB
FROM table1, table2
WHERE value = $value
and table1.uid = table2.uid
或者你可以通过
加入表格SELECT
SQL_CALC_FOUND_ROWS ColA,
ColB
FROM table1 INNER JOIN table2 on table1.uid = table2.uid
WHERE value = $value