SQL JOIN不返回任何结果

时间:2019-07-08 18:14:23

标签: php mysql sql

if(isset($_GET['id']) && $_GET['id'] != null) {

    $id = $_GET['id'];

    $sql = "SELECT 
    `maps.name`,
    `maps.description`,
    `maps.date`,
    `maps.mcversion`,
    `maps.mapid`,
    `maps.category`,
    `maps.format`,
    `users.username`,
    `users.rank`,
    `users.verified`,
    `users.mcusername`,
    COUNT(`views.mapid`) AS `views`,
    COUNT(`likes.mapid`) AS `likes`,
    COUNT(`downloads.mapid`) AS `downloads`,
    COUNT(`subscribes.channelid`) AS `subscribers`
    FROM  `maps` INNER JOIN `users` ON `maps.userid` = `users.id` 
        INNER JOIN `views` ON `maps.mapid` = `views.mapid`
        INNER JOIN `likes` ON `maps.mapid` = `likes.mapid`
        INNER JOIN `downloads` ON `maps.mapid` = `downloads.mapid`
        INNER JOIN `subscribe` ON `mapid.userid` = `subscribe.channelid`
    WHERE `maps.mapid` = '$id'";

    $result = mysqli_query($con,$sql);

    if (mysqli_num_rows($result) > 0)   {
        echo “success”;
    } else {
        header("LOCATION: index.php");
    }

    $sql = "SELECT * FROM `maps` WHERE `id`=$id";
    $result = mysqli_query($con,$sql);

    if (mysqli_num_rows($result) > 0)   {
        viewer($id);
    } else {
        header("LOCATION: index.php");
    }

这可行,但是我需要更多表中的数据。

$sql = "SELECT 
    `maps.name`,
    `maps.description`,
    `maps.date`,
    `maps.mcversion`,
    `maps.mapid`,
    `maps.category`,
    `maps.format`,
    `users.username`,
    `users.rank`,
    `users.verified`,
    `users.mcusername`,
    COUNT(`views.mapid`) AS `views`,
    COUNT(`likes.mapid`) AS `likes`,
    COUNT(`downloads.mapid`) AS `downloads`,
    COUNT(`subscribes.channelid`) AS `subscribers`
    FROM  `maps`
        INNER JOIN `users`     ON `maps.userid` = `users.id` 
        INNER JOIN `views`     ON `maps.mapid` = `views.mapid`
        INNER JOIN `likes`     ON `maps.mapid` = `likes.mapid`
        INNER JOIN `downloads` ON `maps.mapid` = `downloads.mapid`
        INNER JOIN `subscribe` ON `mapid.userid` = `subscribe.channelid`
    WHERE `maps.mapid` = '$id'";

此sql连接好吗?为什么它不返回任何结果?

使用普通的$sql = "SELECT * FROM maps WHERE id=$id";一切正常,但是我也需要其他表中的数据。

2 个答案:

答案 0 :(得分:0)

解决方案:

$sql = "SELECT 
    maps.name,
    maps.description,
    maps.date,
    maps.mcversion,
    maps.mapid,
    maps.category,
    maps.format,
    users.username,
    users.rank,
    users.verified,
    users.mc_username,
    (SELECT COUNT(*) FROM likes WHERE likes.mapid = maps.id) AS likes,
    (SELECT COUNT(*) FROM downloads WHERE downloads.mapid = maps.id) AS downloads,
    (SELECT COUNT(*) FROM subscribe WHERE subscribe.channelid = maps.userid) AS subscribers,
    (SELECT COUNT(*) FROM views WHERE views.mapid = maps.id) AS viewers
    FROM  maps
    INNER JOIN users 
        ON maps.userid = users.id
    WHERE maps.id = '$id'";

感谢您的帮助!

答案 1 :(得分:0)

如果您想保护复杂的sql语句,该怎么做? 可以的版本吗?:

if(isset($_GET['id']) && $_GET['id'] != null) {
  $id = $_GET['id'];

  $stmt = $mysqli->prepare('SELECT id FROM maps WHERE id = ?');
  $stmt->bind_param('i', $id);

  $stmt->execute();

  $result = $stmt->get_result();

  if (mysqli_num_rows($result) == 1)    {
    $row = $result->fetch_assoc();
      $secid = $row["id"];
  } else {
      echo "error2";
  }

  $sql = "SELECT 
  maps.name,
  maps.description,
  maps.date,
  maps.mcversion,
  maps.mapid,
  maps.category,
  maps.format,
  users.username,
  users.rank,
  users.verified,
  users.mc_username,
  (SELECT COUNT(*) FROM likes WHERE likes.mapid = maps.id) AS likes,
  (SELECT COUNT(*) FROM downloads WHERE downloads.mapid = maps.id) AS downloads,
  (SELECT COUNT(*) FROM subscribe WHERE subscribe.channelid = maps.userid) AS subscribers,
  (SELECT COUNT(*) FROM views WHERE views.mapid = maps.id) AS viewers
  FROM maps
  INNER JOIN users 
      ON maps.userid = users.id
  WHERE maps.id = '$secid'";

  $result = mysqli_query($con,$sql);

  if (mysqli_num_rows($result) > 0) {
      $row = mysqli_fetch_assoc($result);
      echo $row["name"];
  } else {
      echo "error3";
  }

} else {
    echo "error1";
}

数据库连接:

$mysqli = new mysqli('127.0.0.1', 'root', 'pass’, 'db’);