php - sql select"其中"等于几个变量?

时间:2016-10-28 18:51:48

标签: php mysql sql json

我正在从JSON链接解析3个变量,每个变量都包含一个电影院名称:

$cinema1
$cinema2
$cinema3

之后,我需要从表格中选择所有3个电影院的ID,然后使用这些ID我需要查找表格" movietimings"看今天是否有电影在播放,如果今天有电影在电影院播放,那么我需要去电影"电影"表并显示有关该电影的所有信息。

所以,这就是已经完成的事情:

    $sql2 = "SELECT DISTINCT * FROM cinema WHERE cinemaname = '$cinema1' AND cinemaname='$cinema2' AND cinemaname='$cinema3' ";

    $result2 = $conn->query($sql2);

    if ($result2->num_rows > 0) {
        // output data of each row
        while($row2 = $result2->fetch_assoc()) {
$cinemaid2 = $row2['id'];// I have received a cinema id




//getting the main id, since the id I just got is a branch of the main cinema, so the main id that is associated with movie timings is down below
$sql5 = "SELECT DISTINCT * FROM cinemas WHERE cinemaname = '$cinemaid2'";

$result5 = $conn->query($sql5);

if ($result5->num_rows > 0) {
    // output data of each row
    while($row5 = $result5->fetch_assoc()) {

$cinemaid = $row5['id'];//received main id required

$today = date("m/d/Y");//getting todays date

//now trying to find if a movie today playing
$sql3 = "SELECT  * FROM moviecinemashowsassociation WHERE cinemaid LIKE '$cinemaid' AND showdate LIKE '$today'";

$result3 = $conn->query($sql3);

if ($result3->num_rows > 0) {
    // output data of each row
    while($row3 = $result3->fetch_assoc()) {
$movieid = $row3['movieid'];// selected a movie id played today

//selecting information about movie
$sql4 = "SELECT  * FROM movie WHERE id='$movieid'";

$result4 = $conn->query($sql4);

if ($result4->num_rows > 0) {
    // output data of each row
    while($row4 = $result4->fetch_assoc()) {

问题是我有3个不同的电影院应该通过一个循环。

1 个答案:

答案 0 :(得分:1)

您正在寻找OR条件而不是AND条件,因为cinemaname在同一时间点不能保存多个值,因此它将是指定的值之一

WHERE cinemaname = '$cinema1' OR cinemaname = '$cinema2' OR cinemaname = '$cinema3'

(或)使用IN运算符

WHERE cinemaname IN ('$cinema1' ,'$cinema2' , '$cinema3')