我真的在努力尝试将这两个查询(下面)和INNER JOIN一起使用。我在_GET中传递一个数组。我想从两个不同的mysql表中选择。
网址数组_GET示例:
www.mysite.com/users_slideshows[]=one&users_slideshows[]=two&users_slideshows[]=three
以下是我尝试合并的两个问题:
$mysqli = new mysqli('localhost','root','1234','root');
$records = array();
foreach ($_GET["users_slideshows"] as $djahjkdsh) {
$result = $mysqli->query("SELECT * FROM playlist_builder WHERE playlist_builder.volume IN ('".$djahjkdsh."') ");
while($row = $result->fetch_array(MYSQL_ASSOC)) {
$records[] = array('tags' => array($row));
}
$json = json_encode($records, JSON_PRETTY_PRINT);
}
foreach ($_GET["users_slideshows"] as $djahjkdsh) {
$result = $mysqli->query("SELECT * FROM custom_slideshow WHERE custom_slideshow.volume IN ('".$djahjkdsh."') ");
while($row = $result->fetch_array(MYSQL_ASSOC)) {
$records[] = array('tags' => array($row));
}
$json = json_encode($records, JSON_PRETTY_PRINT);
}
如果我只从一张桌子中选择,它就会很完美,但只有当我将两者结合在一起时才能完美。
我已尝试过以下内容......没有发生
SELECT * FROM playlist_builder INNER JOIN custom_slideshow ON custom_slideshow.volume = playlist_builder.volume IN ('".$djahjkdsh."')
如果有人能帮我解决我做错的事情,我会非常感激。谢谢!
答案 0 :(得分:1)
您的SQL语法不正确,因为您在同一逻辑ON
子句中运行了两个表达式。只需将ON
子句分成多个表达式即可。此外,使用一个值,使用IN
上的等式。
SELECT * FROM playlist_builder
INNER JOIN custom_slideshow ON custom_slideshow.volume = playlist_builder.volume
AND playlist_builder.volume = ?)
或者,使用WHERE
子句:
SELECT * FROM playlist_builder
INNER JOIN custom_slideshow ON custom_slideshow.volume = playlist_builder.volume
WHERE playlist_builder.volume = ?)
PHP (使用parameterization和表别名)
$sql = 'SELECT * FROM playlist_builder p
INNER JOIN custom_slideshow c ON c.volume = p.volume AND p.volume = ?';
foreach ($_GET["users_slideshows"] as $djahjkdsh) {
$stmt = $mysqli->prepare($sql);
$stmt->bind_param("s", $djahjkdsh);
$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_assoc()) {
$records[] = array('tags' => array($row));
}
$json = json_encode($records, JSON_PRETTY_PRINT);
$stmt->close();
}