简体   繁体   中英

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.

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

You are using $sql variable in

$result = $con->query($sql);

It should be $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.

Join Types:

INNER JOIN: Returns records that have matching values in both tables. 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.

LEFT JOIN: Return all records from the left table, and the matched records from the right table. That means all rows in Table A will match either the value is included in table B or not.

RIGHT JOIN: Return all records from the right table, and the matched records from the left table. That means all rows in Table B will match either the value is included in table A or not.

FULL JOIN: Return all records when there is a match in either left or right table. All rows in table A and table B will be included in te result set either they match or not.

Your SQL query may look like:

$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.

I think this thread also helps you: Join two mysql tables with php

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:

$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). If tableB.name=tableA.username are not columns with same value you can join by other columns, like assign1, or assign2:

assign1:

$queryagain = mysqli_query("SELECT * FROM tableB INNER JOIN tableA ON tableB.assign1=tableA.assign1 WHERE tableA.assign2 = tableB.assign2");

assign2:

$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.

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