简体   繁体   English

如何从第一个表中返回所有列,而从第二个表中只返回一列

[英]How to return all columns from 1st table and only 1 column from 2nd table

I have a first table called "purchases"我有一张名为“purchases”的表

news_series, transaction_id, owner_id, amount

I have another table called "Events"我有另一个名为“事件”的表

news_name, news_id, series_id, news_description

The issue that I'm running into is that if I do我遇到的问题是,如果我这样做

purchases.news_series joins to events.series_id

The issues is that there can be multiple events with the series id....问题是系列 id 可以有多个事件....

I need to join just one to get the news_name from the joined table so the base select is我只需要加入一个即可从连接表中获取news_name ,因此基本选择是

Select * from purchases where owner_id=29

140, asldkfj_sdfx34, 29, 40

then I add the joined table然后我添加连接表

Select * 
from purchases 
LEFT JOIN events on purchases.news_series=events.series_id 
where owner_id=29

140, asldkfj_sdfx34, 29, 40,"THIS EVENT", 606, 140, "MY FIRST EVENT"
140, asldkfj_sdfx34, 29, 40,"THIS EVENT", 607, 140, "MY FIRST EVENT"

and I end up with a few rows returned...I just need one to capture the new_name from the events table.我最终返回了几行......我只需要一个来从事件表中捕获 new_name 。

I just need one to capture the news_name from the events table.我只需要一个来从事件表中捕获 news_name 。

This is what I would do:这就是我会做的:

PURCHASES TABLE:采购表:

+-------------+----------------+----------+--------+
| news_series | transaction_id | owner_id | amount |
+-------------+----------------+----------+--------+
| 140         | asldkfj_sdfx34 | 29       | 40     |
+-------------+----------------+----------+--------+

EVENTS TABLE:事件表:

+------------+---------+-----------+------------------+
| news_name  | news_id | series_id | news_description |
+------------+---------+-----------+------------------+
| THIS EVENT | 606     | 140       | MY FIRST EVENT   |
+------------+---------+-----------+------------------+
| THIS EVENT | 607     | 140       | MY FIRST EVENT   |
+------------+---------+-----------+------------------+

SELECT DISTINCT just the one column you want from the joined table: SELECT DISTINCT只是你想要从连接表中的一列:

SELECT DISTINCT p.*, e.news_name
FROM Purchases p
LEFT JOIN Events e ON p.news_series = e.series_id
WHERE p.owner_id = 29

在此处输入图片说明


If you do not SELECT DISTINCT , this is why you are getting two rows.如果你没有SELECT DISTINCT ,这就是你得到两行的原因。

在此处输入图片说明


Test:测试:

;WITH Purchases (news_series, transaction_id, owner_id, amount) AS (
    SELECT '140','asldkfj_sdfx34','29','40'
), Events (news_name,news_id,series_id,news_description) AS (
    SELECT 'THIS EVENT','606','140','MY FIRST EVENT' UNION ALL
    SELECT 'THIS EVENT','607','140','MY FIRST EVENT' )

SELECT DISTINCT p.*, e.news_name
FROM Purchases p
LEFT JOIN Events e ON p.news_series = e.series_id
WHERE p.owner_id = 29

Might I suggest:我可以建议:

Select p.*,
       (select e.news_name
        from events e
        where p.news_series = e.series_id 
        limit 1
       )
from purchases  p
where owner_id = 29;

This is guaranteed to return one row per purchase.这保证每次购买返回一行。

暂无
暂无

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

相关问题 Mysql - 如何根据第一个表的 2 列的值显示第二个表中的列? - Mysql - How to display column from 2nd table based on values from 2 columns of 1st table? 在mySql join语句中显示第1个表的所有结果,但仅显示第2个表的第一个结果 - Show all results from 1st table but only 1st result from 2nd table in mySql join statement mySQL 如何从第一个表中选择不同的(1 列:pruduct_Type)并从第二个表中选择 2 列 - mySQL How to select distinct(1 column : pruduct_Type) from 1st table and select 2 columns from 2nd table 如何将某些列数据从第一表移动到第二表。 并在单个查询中用不同的数据填充第二张表的其他列 - How to move certain column data from 1st table to 2nd table. And filling other columns of 2nd table with different data in a single query 如何在同一选择语句中从第一张表的列值中选择第二张表的数据? - How to select data of 2nd table from column values of 1st table in same select statement? 是否可以使用PHP PDO从第一张表中的列从第二张表中获取数据? - Is it possible to get data from the 2nd table using column in 1st table using PHP PDO? 如何从第二个表中显示2个不同的ID从第一个表中显示ID - How to show id from 1st table from 2 different ids from 2nd table 如何使用PHP偏移HTML表以从不同列的第二行单元格值中减去第一行的单元格值 - How To offset an HTML table with PHP to Subtract cell value of 1st row from 2nd row cell value of different columns 引用从第一张表到第二张表的多个行ID,以便每次可以显示第一张表中的所有行 - Referencing multiple row id from 1st table to 2nd table so that it can show all rows from the 1st table every time Codeigniter联接3个表并从第1个表获取数据,其中第1个表与第2个表链接,第2个表与第3个表链接 - codeigniter join 3 tables and get data from 1st table where 1st table is linked with 2nd table and 2nd table is linked with 3rd table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM