简体   繁体   English

使用第一个表的输出从第二个表中选择特定数据

[英]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. 我想要做的是,我需要选择一个nameclient使用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_idclient表中选择name

account table account
帐户表

client table client
在此处输入图片说明

I already take a look at join and left join and I'm still stuck. 我已经看过joinleft 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.

相关问题 MySQL CodeIgniter是否基于第二个表的第一个Select列进行选择 - MySQL CodeIgniter Select or not from second table based on column of first Select 如何将数据从第一个表插入第二个表,就像这个例子 - how to insert the data from the first table to the second table to be like this example 如果第一个表中不存在,我想从第二个表中获取数据 - I want fetch data from second table if not present in first table 如何从两个表中选择数据,而两个表只为第一表的一行选择了第二表的第一行 - How to SELECT data from two tables which only first row of second table is selected for one row of first table 使用DOM从第二个HTML表中提取数据,忽略第一个表 - Pull data from second HTML table using DOM, ignore first table 从与第一条记录匹配的表中选择第二条记录 - Select second record from table matching with first record 从表中选择数据并从另一个表中填充特定值 - select data from a table and fill specific value from another table 我想基于json-php中输出的第一张表行从第二张表获得输出结果 - I want to get output result from second table based on first table rows output in json-php 使用来自第一个MySQL查询的结果从第二个表中提取数据 - Extract data from second table using results from first MySQL query select 来自使用特定行值的表 - select from table using an specific row values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM