LEFT JOIN时选择自定义字段和所有字段

时间:2016-03-22 06:17:36

标签: php mysql

我正在从2个表中选择数据。

$sql = "SELECT tb1.id, tb2.name FROM tblA tbl1 LEFT JOIN tblB tbl2 ON tb1.id = tbl2.studentID ORDER BY tbl1.id DESC LIMIT 20";
$statement = $con_db->prepare($sql);

我现在的问题是,我可以从tb1和tb2中的所有字段中选择自定义字段吗? e.g。

$sql = "SELECT tb1.id, tb1.subject, tb2.(*) FROM ....";

1 个答案:

答案 0 :(得分:2)

您可以编写如下代码

$sql = "SELECT tb1.id, tb1.subject, tb2.* FROM tblA tbl1 LEFT JOIN tblB tbl2 ON tb1.id = tbl2.studentID ORDER BY tbl1.id DESC LIMIT 20";