[英]Select specific data from second table using output from first table
I have two table. 我有两个桌子。 One is
account
and the other is client
table. 一个是
account
,另一个是client
表。 The user will give input and in this case is 19
. 用户将提供输入,在这种情况下为
19
。 What I want to do is that I want to select ONLY a single name
from client
using client_id
as identifier and only using a single select statement. 我想要做的是,我只需要选择一个
name
从client
使用client_id
作为标识,只能用一个 SELECT语句。 I could do select query twice but that's not what I wanted. 我可以选择两次查询,但这不是我想要的。
So to explain it better: User give input such as $id = 19
, I will use id
from account
table to identify client_id
which in this case is 17
. 所以更好地解释一下:用户输入$ id =
19
,我将使用account
表中的id
来标识client_id
,在这种情况下为17
。 I will then select name
from client
table using client_id
. 然后,我将使用
client_id
从client
表中选择name
。
account
table account
表
client
table client
表
I already take a look at join
and left join
and I'm still stuck. 我已经看过
join
和left join
,但我仍然很困难。 Please help me with this. 请帮我解决一下这个。 Thanks in advance.
提前致谢。
My current not working code: 我当前无法正常工作的代码:
$sql = " SELECT account.id, client.client_id\n"
. " FROM account, client\n"
. " WHERE account.id = 19";
A simple inner join will do the task 一个简单的内部联接就可以完成任务
$query = "select
account.id,client.client_id
from
account
join
client
on
account.client_id = client.client_id
where
account.id = 19";
This should work using an INNER JOIN
: 这应该使用
INNER JOIN
起作用:
SELECT c.name
FROM Client C
INNER JOIN Account A ON C.Client_Id = A.Client_Id
WHERE A.Id = 19
You're query is producing a Cartesian Product since you aren't joining the tables on any field. 您要查询的是生成笛卡尔乘积,因为您没有联接任何字段上的表。
You didn't join Account and Client. 您尚未加入帐户和客户。 And moreover you want only the client name and the query has to be:
而且,您只需要客户端名称,查询必须是:
Select distinct client.client_name from account, client where client.client_id=account.account_id and account_id=19
You need to specify which column is used to join the two tables. 您需要指定用于连接两个表的列。
It's an unfortunate fact about the SQL language that the default behavior is to join two tables by matching each row in one table to every row in the other table. 关于SQL语言,不幸的事实是默认行为是通过将一个表中的每一行与另一表中的每一行匹配来联接两个表。 That is, every possible pairing is part of the result set.
也就是说,每个可能的配对都是结果集的一部分。 SQL doesn't have any way of inferring which column(s) you want to use as the join criteria, so you have to specify the condition yourself.
SQL没有任何方法可以推断要用作连接条件的列,因此您必须自己指定条件。
Here's a better form for the query: 这是查询的更好形式:
$sql = " SELECT account.id, client.client_id
FROM account JOIN client USING (client_id)
WHERE account.id = 19";
Do use the JOIN
syntax. 不要使用
JOIN
语法。 It has been the standard way of doing joins in SQL since 1992. The comma-style syntax is only for backward compatibility with pre-1992 SQL. 自1992年以来,它一直是SQL中进行连接的标准方法。逗号式语法仅用于与1992年前的SQL向后兼容。 They perform exactly the same, but the 1992 syntax can do outer joins and it's easier to read.
它们的表现完全相同,但是1992语法可以进行外部联接,并且更易于阅读。
PS: PHP supports multi-line strings, so there's no need to concatenate strings together as you're doing. PS:PHP支持多行字符串,因此您无需在进行操作时将字符串连接在一起。 That's a Java habit.
这是Java习惯。
$sql = " SELECT account.id, client.client_id
FROM `account`
INNER JOIN `client` ON `client`.client_id = `account`.`client_id`
WHERE account.id = 19";
Since you didn't provide information about the structure of your client table, this is just a guess: 由于您未提供有关客户表结构的信息,因此这只是一个猜测:
SELECT account.id, client.client_id
FROM account, client
WHERE account.id = 19
AND account.client_id = client.id
Maybe you should try: 也许您应该尝试:
$sql = "SELECT account.id, client.client_id
FROM account, client
WHERE account.client_id=client.id AND account.id = 19 "
这应该为您完成。从帐户c.name,c.client_id,a.id到客户端c的左连接上a.client_id = c.client_id,其中a.id = 19
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.