简体   繁体   English

使用INNER JOIN / JOIN进行多个查询

[英]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] 目前,我要匹配的是law_case的大小写![在此处输入图片描述] [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. 而且它所做的只有一个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. 我想用INNER JOIN,JOIN或LEFT JOIN和使用,而只看得到不同MEMBER_NAME和url每个network_member或谁的人有来自同一expertise_id individual_expertise表。

I'm new to JOIN and tried this code but it doesnt work: 我是JOIN新手,并尝试过此代码,但它不起作用:

$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. 您需要获得JOIN类型的概述。 You have to build a query using INNER, LEFT, RIGHT, FULL joins depending on your logical requirements. 您必须根据您的逻辑需求使用INNER, LEFT, RIGHT, FULL连接来构建查询。 You can assume that INNER join is equal to && or AND operators in conditional statements, I mean records must match in both tables. 您可以假设条件语句中的INNER联接等于&&AND运算符,我的意思是两个表中的记录必须匹配。 While, LEFT join will return all rows from left table of join and matched rows from right table of join. 同时, LEFT将从联接的左表返回所有行,并从联接的右表返回匹配行。 And RIGHT is inverse of LEFT . RIGHTLEFT倒数。 And FULL join is an example of || FULL join是||的示例 or OR in operators in conditional statements. 或条件运算符中的运算符中的OR 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? 您知道INNER JOIN和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 = ?

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

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