简体   繁体   中英

Use INNER JOIN / JOIN for multiple query

What I have at the moment is to match the case from law_case ![enter image description here][1]

$query1 = "SELECT * FROM law_case WHERE id =?";
$query1vals = array($_GET['id']);
$ids = $adb->selectRecords($query1, $query1vals, false);
$a = $ids['case_type_id'];
$b = $ids['funding_pref'];
#
$query2 = "SELECT type_name FROM case_type WHERE id =?";
$query2vals = array($a);
$ids1 = $adb->selectRecords($query2, $query2vals, false);
$d = $ids1['type_name'];
#
$query3 = "SELECT * FROM expertise WHERE expertise_desc =?";
$query3vals = array($d);
$ids2 = $adb->selectRecords($query3, $query3vals, false);
$c = $ids2['id'];
#
$query4 = "SELECT * FROM individual_expertise WHERE expertise_id =?";
$query4vals = array($c);
$ids3 = $adb->selectRecords($query4, $query4vals, false);
$e = $ids3['individual_id'];
#
$query5 = "SELECT * FROM individual WHERE id =?";
$query5vals = array($e);
$ids4 = $adb->selectRecords($query5, $query5vals, false);
$f = $ids4['network_member_id'];
#
$query6 = "SELECT * FROM network_member WHERE id =?";
$query6vals = array($f);
$ids5 = $adb->selectRecords($query6, $query6vals, false);

And what it does it it only gets one network_member. I want to use INNER JOIN, JOIN or LEFT JOIN and use a while look to get the different member_name and url for each network_member or who's individual has the same expertise_id from individual_expertise table.

I'm new to JOIN and tried this code but it doesnt work:

$sql = "SELECT member_name, url
FROM individual_expertise
LEFT JOIN individual
USING (individual_id)
LEFT JOIN network_member
USING (network_member_id)
WHERE expertise_id = ?";
$ids3 = $adb->selectRecords($sql, $query4vals, false);

echo $ids3['member_name'];

You need to get an overview of JOIN types. You have to build a query using INNER, LEFT, RIGHT, FULL joins depending on your logical requirements. You can assume that INNER join is equal to && or AND operators in conditional statements, I mean records must match in both tables. While, LEFT join will return all rows from left table of join and matched rows from right table of join. And RIGHT is inverse of LEFT . And FULL join is an example of || or OR in operators in conditional statements. I mean either a match in both tables. So you have to join depending on logic you require.

See here

Do you know the difference between INNER JOIN and LEFT JOIN? I think you don't. You need an INNER JOIN here.

I think this is the right solution for your question:

SELECT
    network_member.member_name,
    network_member.url
FROM
    network_member
INNER JOIN
    individual ON individual.network_member_id = network_member.id
INNER JOIN
    individual_expertise ON individual_expertise.individual_id = individual.id
WHERE
    individual_expertise.expertise_id = ?

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