[英]Query to fetch data from two different tables
I have two tables.我有两张桌子。 Table A and B. I have a log-in form where I will get the username,password, assign, assign2 of the user from table A and fetch data depending on the user's assign1 and assign2 from table B.表 A 和 B。 我有一个登录表单,我将从表 A 中获取用户的用户名、密码、assign、assign2,并根据用户的assign1 和assign2 从表B 中获取数据。
How my query would look like?我的查询会是什么样子? Looking for answers.寻找答案。 Thankyou so much.非常感谢。
Table A -THIS IS THE TABLE FOR LOG-IN FORM
==========================================
username | password | assign1 | assign2 |
------------------------------------------
SANDRA | SANTOS | 1 | 1 | //Values
--------------------------------------------
Table B -
=======================================
name | assign1 | assign 2 |
------------------------------
DADA | 1 | 1 | //this will be displayed
------------------------------
gorg | 2 | 2 |
//this must not be displayed since the user assign1 and assign2 who logged in did not match to this //this不能显示,因为登录的用户assign1和assign2与此不匹配
You are using $sql variable in您正在使用 $sql 变量
$result = $con->query($sql); $result = $con->query($sql);
It should be $queryagain.它应该是 $queryagain。
I think your are very very new to database programing.我认为您对数据库编程非常陌生。 First of all I recomend you to read https://www.w3schools.com/sql/sql_join.asp this page and work on examples using try it yourself part.首先,我建议您阅读https://www.w3schools.com/sql/sql_join.asp此页面,并使用亲自尝试部分来处理示例。
Join Types:加盟类型:
INNER JOIN: Returns records that have matching values in both tables. INNER JOIN:返回在两个表中具有匹配值的记录。 That means if assign field is filled in Table A and the value is included in table B then this type of join should match the rows.这意味着如果分配字段填充在表 A 中并且值包含在表 B 中,那么这种类型的连接应该与行匹配。
LEFT JOIN: Return all records from the left table, and the matched records from the right table. LEFT JOIN:返回左表中的所有记录,以及右表中匹配的记录。 That means all rows in Table A will match either the value is included in table B or not.这意味着表 A 中的所有行都将匹配该值是否包含在表 B 中。
RIGHT JOIN: Return all records from the right table, and the matched records from the left table. RIGHT JOIN:返回右表中的所有记录,以及左表中匹配的记录。 That means all rows in Table B will match either the value is included in table A or not.这意味着表 B 中的所有行都将匹配该值是否包含在表 A 中。
FULL JOIN: Return all records when there is a match in either left or right table. FULL JOIN:当左表或右表中存在匹配时,返回所有记录。 All rows in table A and table B will be included in te result set either they match or not.表 A 和表 B 中的所有行都将包含在结果集中,无论它们是否匹配。
Your SQL query may look like:您的 SQL 查询可能如下所示:
$sql = "SELECT * FROM table_a a INNER JOIN table_b b on a.assign1 = b.assign1 INNER JOIN table_b b2 on a.assign2 = b2.assign2";
execution part of the sql can be different depending your database engine or other libraries. sql 的执行部分可能会有所不同,具体取决于您的数据库引擎或其他库。
I think this thread also helps you: Join two mysql tables with php我认为这个线程也可以帮助你: 用 php 连接两个 mysql 表
Your question is not clear, but if you want to fetch data from Table B
which depends on assign1
and assign2
you can do it like this:你的问题不清楚,但如果你想从依赖于assign1
和assign2
Table B
获取数据,你可以这样做:
$queryagain = mysqli_query("SELECT * FROM tableB INNER JOIN tableA ON tableB.name=tableA.username WHERE tableA.assign1 = tableB.assign1 AND tableA.assign2 = tableB.assign2");
When you use JOIN
it's best practice to JOIN
by primary key (id).使用JOIN
,最佳做法是按主键 (id) 进行JOIN
。 If tableB.name=tableA.username
are not columns with same value you can join by other columns, like assign1, or assign2:如果tableB.name=tableA.username
不是具有相同值的列,您可以通过其他列加入,例如assign1或assign2:
assign1:分配1:
$queryagain = mysqli_query("SELECT * FROM tableB INNER JOIN tableA ON tableB.assign1=tableA.assign1 WHERE tableA.assign2 = tableB.assign2");
assign2:分配2:
$queryagain = mysqli_query("SELECT * FROM tableB INNER JOIN tableA ON tableB.assign2=tableA.assign2 WHERE tableA.assign1 = tableB.assign1");
Notice: Where clause is not necessay, you can edit WHERE clause depends of the result you want.注意:Where 子句不是必需的,您可以根据您想要的结果编辑 WHERE 子句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.