我通常会这样做:
$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
他们已经加入了。它与
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;
请注意:您应该始终使用后面的语法,用 withJOIN
来连接表而不是WHERE
子句。这是执行此操作的推荐方法。有关更多信息,请参阅此帖子。
请试试这个
$query = "SELECT table1.ColA, table2.ColC FROM table1,
table2 WHERE table1.uid = table2.uid and table1.value=$value";
或者
$query = "SELECT table1.ColA, table2.ColC FROM table1 INNER JOIN table2
ON (table1.uid = table2.uid) WHERE table1.value=$value";
试试这个 ::
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