[英]SELECT * FROM table_1 WHERE id = id in table_2
Been working on this for a while; 已经为此工作了一段时间; Need to select data from a table where the ID column matches the ID from another table.
需要从一个ID列与另一个表的ID匹配的表中选择数据。
My code so far: 到目前为止,我的代码:
<?php
$sql = "SELECT * FROM input";
$sql2 = "SELECT * FROM output WHERE question_id =".$row["id"];
$result = $conn->query($sql);
$result2 = $conn->query($sql2);
if ($result->num_rows > 0) {
$index = 0;
while($row = $result->fetch_assoc()) {
$index++;
?>
<?php
echo '<input type="hidden" name="questionid" value="'. $row['id'].'"/>';
?>
<?php
if ($result2->num_rows > 0) {
while($row2 = $result2->fetch_assoc()) {
}
} else {
echo "0 results";
}
}
} else {
echo "0 results";
}
$conn->close();
?>
I know I m missing something important here. 我知道我在这里错过了一些重要的事情。 Any suggestions are appreciated.
任何建议表示赞赏。
In order to achieve this thing you can use the JOIN query which is available in mysql. 为了实现这一目标,您可以使用mysql中可用的JOIN查询。
Ex: Consider two tables user
and course
例如:考虑两个表的
user
和course
User Table: 用户表:
id name course
1 Alice 1
2 Bob 1
3 Caroline 2
4 David 5
5 Emma (NULL)
Course Table: 课程表:
id name
1 HTML5
2 CSS3
3 JS
4 PHP
5 WORDPRESS
INNER JOIN (or just JOIN) 内部联接(或仅联接)
The most frequently used clause is INNER JOIN. 最常用的子句是INNER JOIN。 This produces a set of records which match in both the user and course tables, ie all users who are enrolled on a course:
这将产生一组记录,这些记录在用户表和课程表中都匹配,即在课程中注册的所有用户:
SELECT user.name, course.name FROM `user` INNER JOIN `course` on user.course = course.id;
LEFT JOIN 左联接
What if we require a list of all students and their courses even if they're not enrolled on one? 如果我们要求列出所有学生及其课程的清单,即使他们没有入学怎么办? A LEFT JOIN produces a set of records which matches every entry in the left table (user) regardless of any matching entry in the right table (course):
LEFT JOIN产生一组记录,该记录与左表(用户)中的每个条目匹配,而与右表(课程)中的任何匹配条目无关:
SELECT user.name, course.name FROM `user` LEFT JOIN `course` on user.course = course.id;
RIGHT JOIN 正确加入
Perhaps we require a list all courses and students even if no one has been enrolled? 也许即使没有人注册,我们也需要列出所有课程和学生的清单? A RIGHT JOIN produces a set of records which matches every entry in the right table (course) regardless of any matching entry in the left table (user):
RIGHT JOIN产生一组记录,该记录与右表(课程)中的每个条目匹配,而与左表(用户)中的任何匹配条目无关:
SELECT user.name, course.name FROM `user` RIGHT JOIN `course` on user.course = course.id;
You missed fetching the row items to extract the ID you want to use as CONSTRAINT in the next query 您错过了提取行项目以提取要在下一个查询中用作CONSTRAINT的ID的操作
$sql = "SELECT * FROM input";
$row = mysqli_fetch_array($sql);
$sql2 = "SELECT * FROM output WHERE question_id =".$row["id"];
$result = $conn->query($sql);
$result2 = $conn->query($sql2);
You can also consider JOIN SELECT of the two tables like this; 您也可以像这样考虑两个表的JOIN SELECT;
$sql = SELECT input.* , output.* FROM input JOIN output (id) WHERE input.id = "".
check out MySql Join three tables 签出MySql加入三个表
不确定这是否是您想要的,因此在拒绝投票之前在评论中让我知道,请尝试以下操作:
SELECT * FROM output, input WHERE output.question_id = input.id
You can achieve this using SQL INNER JOIN Keyword 您可以使用SQL INNER JOIN关键字来实现
SQL SQL
SELECT * FROM input INNER JOIN output on input.id = output.question_id
Use this inner join to get row from both table where the ids are match 使用此内部联接从ID匹配的两个表中获取行
SELECT *
FROM input
INNER JOIN output
ON input.id=output.question_id;
You should rather join both tables with an INNER JOIN
, which will discard rows from tables if the id
is not present in the other table. 您应该使用
INNER JOIN
两个表,如果另一个表中没有该id
则该表将丢弃表中的行。 Try something like: 尝试类似的方法:
SELECT * FROM tbl1 INNER JOIN tbl2 ON tbl1.id = tbl2.id;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.