简体   繁体   English

从多个表中获取具有相同名称的列

[英]get column with same name from multiple tables

i have two tables: USERS(id, name, email, password) QUESTIONS(id, userid, type, ques, date, time) 我有两个表:USERS(id,名称,电子邮件,密码)QUESTIONS(id,userid,类型,ques,日期,时间)

i have to pass the value of questions.id as qid. 我必须将Questions.id的值作为qid传递。 i am unable to get the desired result with the following code. 我无法使用以下代码获得所需的结果。 instead of taking 'id' value of QUESTIONS table, it is taking the 'id' value of USERS table, and therby producing wrong output. 而不是采用QUESTIONS表的'id'值,而是采用USERS表的'id'值,从而产生错误的输出。

$query = "
SELECT *
     , q.id qid 
  FROM questions q
  JOIN users u
    ON u.id = q.userid 
 WHERE type = 'technical' 
 ORDER 
    BY date
     , time DESC
 ";
$result = mysqli_query($conn,$query);
while ($row = mysqli_fetch_array($result)) {

$id = $_SESSION['id'];  
$ques=$row['question'];
$user = $row['username'];

    echo strtoupper($user);

    echo "<a href='allview.php?qid=$row[id]' class='class4'> $ques </a>";

}
?>

This code: 这段代码:

$id = $_SESSION['id'];  

Refers to the id column in the select . 引用selectid列。 That id comes from the users table. id来自users表。

You have renamed the question id to qid , so try: 您已将问题ID重命名为qid ,因此请尝试:

$qid = $_SESSION['qid'];

A word of advice: only fetch the columns you are actually going to use: 忠告:仅获取您实际要使用的列:

SELECT u.username, q.id as qid
FROM questions q JOIN
     users u
     ON u.id = q.userid
WHERE type = 'technical' 
ORDER BY date, time DESC;

The * is handy when debugging, but the code could end up behaving unexpectedly at some point because the underlying table structure changes. *在调试时很方便,但是由于基础表结构发生了变化,因此代码有时可能会出乎意料的表现。

Try to use this code 尝试使用此代码

$query = "
SELECT *
     , q.id AS qid,
       q.id AS id
  FROM questions q

Try using this 试试这个

$query = "
SELECT *
     , questions.id qid 
  FROM questions q
  , users u
 WHERE q.userid = u.id
 AND type = 'technical' 
 ORDER 
    BY date
     , time DESC
 ";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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