Php 如果mySQL中的两个表的列名称相同,如何连接这两个表

Php 如果mySQL中的两个表的列名称相同,如何连接这两个表,php,mysql,sql,Php,Mysql,Sql,错误-致命错误:未捕获PDOException:SQLSTATE[23000]:完整性约束冲突:where子句中的1052列“f_id”不明确 如何解决 谢谢您需要在此处指定要引用的表的f_id <?php error_reporting(E_ALL); ini_set('display_errors', 1); require_once 'admin/include/dbconfig.php'; $counter = 0; $stmt = $DB_con->

错误-致命错误:未捕获PDOException:SQLSTATE[23000]:完整性约束冲突:where子句中的1052列“f_id”不明确

如何解决

谢谢

您需要在此处指定要引用的表的f_id

<?php
  error_reporting(E_ALL);
  ini_set('display_errors', 1);

  require_once 'admin/include/dbconfig.php';

  $counter = 0;

  $stmt = $DB_con->prepare('SELECT f_category.f_route, f_content.f_doc, 
  f_content.f_thumb, f_content.f_title FROM f_content, f_category WHERE 
  f_content.f_id = f_category.f_id AND f_id = :uid ORDER BY f_content.id 
  DESC');

  $stmt->bindParam(':uid',$id, PDO::PARAM_INT);
  $stmt->execute();

  while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  extract($row);
?>

f_id in…和f_id=:uid。。。如果引用此处的任何一个表,即f_content.f_id或f_category.f_id

在列之前使用表名或使用表别名,则两个表都有一个列f_id,因此在where子句中,如果未为表指定表名或别名,则其不明确

SELECT f_category.f_route, f_content.f_doc, f_content.f_thumb,f_content.f_title 
FROM f_content, f_category 
WHERE f_content.f_id = f_category.f_id AND f_content.f_id = :uid
ORDER BY f_content.id DESC

您可以为此使用别名

此外,在AND运算符之后的列名上没有使用别名

SELECT b.f_route, a.f_doc,a.f_thumb, a.f_title 
FROM f_content a
JOIN f_category b ON a.f_id = b.f_id
WHERE  a.f_id = :uid 
ORDER BY a.id DESC
试试这个查询! 您可以创建表的别名

$stmt = $DB_con->prepare('SELECT b.f_route, a.f_doc, a.f_thumb, a.f_title FROM 
    f_content a, f_category b WHERE a.f_id = b.f_id AND a.f_id = :uid ORDER BY 
    a.id DESC');

您是否尝试使用别名?我已将f_id=:uid更改为f_content.f_id=:uid@ShivamVerma很棒:另外,请不要忘记投票/接受有帮助的答案。
SELECT 
       fca.f_route      , 
       fco.f_doc        , 
       fco.f_thumb      ,
       fco.f_title 
    FROM 
       f_content    fco , 
       f_category   fca
    WHERE 
       fco.f_id  =  fca.f_id 
    AND 
       fca.f_id  = :uid 
    ORDER BY 
       fco.id DESC