我尝试LEFT JOIN
我的查询,但它返回了错误,如此
查询无效:您的SQL语法出错;查看与
附近使用正确的语法MariaDB
服务器版本对应的手册,以便在WHERE `data_binatang`.`id_binatang` = `data_waktu_vaksinasi`.`id_binatang`'
第1行
但是当我只写JOIN时,它返回了json值
这是我的代码:
<?php
include "../mainmenu/koneksi.php";
$rows = array();
$query = "SELECT `data_binatang`.`nama_binatang`, (SELECT min(`data_waktu_vaksinasi`.`tanggal_vaksin`) FROM `data_waktu_vaksinasi` WHERE `data_binatang`.`id_binatang` = `data_waktu_vaksinasi`.`id_binatang`) AS tanggal_awal FROM `data_binatang` JOIN `data_waktu_vaksinasi` WHERE `data_binatang`.`id_binatang` = `data_waktu_vaksinasi`.`id_binatang`";
$query = iconv("UTF-8","UTF-8//IGNORE",$query);
$result = mysql_query($query);
if (!$result)
{
die('Invalid query: ' . mysql_error());
}
while($row = mysql_fetch_assoc($result))
{
$rows[] = $row;
}
echo '{"data_vaksinasi_menu":'.json_encode($rows).'}';
mysql_close($connection);
?>
有人可以帮忙吗?
答案 0 :(得分:1)
在加入后将where
转换为on
JOIN `data_waktu_vaksinasi` on `data_binatang`.`id_binatang` = `data_waktu_vaksinasi`.`id_binatang`";
答案 1 :(得分:0)
在SQL JOIN中,您应使用JOIN
而不是ON
子句指定WHERE
条件。
MySQL允许您在执行ON
时跳过JOIN
条件,但是,在执行ON
时,它不允许您跳过LEFT JOIN
。
试试这个:
SELECT `data_binatang`.`nama_binatang`,
(SELECT min(`data_waktu_vaksinasi`.`tanggal_vaksin`)
FROM `data_waktu_vaksinasi`
WHERE `data_binatang`.`id_binatang` = `data_waktu_vaksinasi`.`id_binatang`) AS tanggal_awal
FROM `data_binatang`
LEFT JOIN `data_waktu_vaksinasi`
ON `data_binatang`.`id_binatang` = `data_waktu_vaksinasi`.`id_binatang`