简体   繁体   English

MySQL与where条件联接

[英]mySQL join with where condition

I have two tables in my db and it is one to many connection with other as shown below 我的数据库中有两个表,它与其他表是一对多连接,如下所示

在此处输入图片说明

In kvves_units table I will get ' name ' from the GET Method 在kvves_units表中,我将从GET方法获得“ name

now I want to have all value from the kvves_units and kvves_members according to the name of the kvves_units 现在我想有从所有值kvves_unitskvves_members根据名称kvves_units

I'm using the code something like this 我正在使用类似这样的代码

$kvvesDetails = $conn->prepare( "SELECT u.id, u.name, u.phone, u.email, u.address, m.name,    m.designantion, m.phone, m.email, m.imageFROM kvves_units AS u JOIN kvves_members AS m ON m.unit_id = u.id WHERE `name` = $committee");

这是一个标准联接:

$kvvesDetails = $conn->prepare( "SELECT u.id, u.name, u.phone, u.email, u.address, m.name, m.designantion, m.phone, m.email, m.image FROM kvves_units AS u JOIN kvves_members AS m ON m.unit_id = u.id WHERE name = '$committee'"

使用此SQL

select kvves_units.*,kvves_members.* from kvves_units a join kvves_members b where a.name = b.name and a.name = '".$_GET['name']."'

Try : 尝试:

$name = $_GET['name'];
$sql = "Select *from kvves_units as u INNER JOIN kvves_members as m where u.id = m.unit_id and u.name = '".$name."'";

You will get your solution. 您将获得解决方案。

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

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