简体   繁体   中英

INNER JOIN : where clause is too ambiguous

I am getting the error "Integrity constraint violation: 1052 Column 'restaurant_id' in where clause is ambiguous' in" .

a) How do I use bindparam on JOIN? Is that even the problem causing this error?

function restaurant(PDO $dbh, $username) {
   global $dbh;
   $stmt = $dbh->prepare("
      SELECT        *
      FROM          users u
      INNER JOIN    menues m
      ON            u.user_id = m.restaurant_id
      INNER JOIN    users_slider s
      ON            m.restaurant_id = s.restaurant_id
      WHERE         restaurant_id = :restaurant_id
   ");

   $stmt->bindParam(":restaurant_id", $_GET['r']);
   $stmt->execute();
   return $stmt->fetchAll();
}

Change

...
WHERE     restaurant_id = :restaurant_id

to

...
WHERE     m.restaurant_id = :restaurant_id
          ^^

请使用m.restaurant_ids.restaurant_id在根据您的表格状况可以解决您的问题的地方。

Always use alias for avoiding conflict in join query . Here restaurant_id should be m.restaurant_id .

Try this query :-

SELECT        *
      FROM          users u
      INNER JOIN    menues m
      ON            u.user_id = m.restaurant_id
      INNER JOIN    users_slider s
      ON            m.restaurant_id = s.restaurant_id
      WHERE         m.restaurant_id =:restaurant_id

put where condition like WHERE m.restaurant_id = :restaurant_id

function restaurant(PDO $dbh, $username) {
   global $dbh;
   $stmt = $dbh->prepare("
      SELECT        *
      FROM      users u
      INNER JOIN    menues m
      ON            u.user_id = m.restaurant_id
      INNER JOIN    users_slider s
      ON            m.restaurant_id = s.restaurant_id
      WHERE     m.restaurant_id = :restaurant_id
   ");

   $stmt->bindParam(":restaurant_id", $_GET['r']);
   $stmt->execute();
   return $stmt->fetchAll();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM