<small id='0kkw0'></small><noframes id='0kkw0'>

  • <legend id='0kkw0'><style id='0kkw0'><dir id='0kkw0'><q id='0kkw0'></q></dir></style></legend>
        <bdo id='0kkw0'></bdo><ul id='0kkw0'></ul>

      <i id='0kkw0'><tr id='0kkw0'><dt id='0kkw0'><q id='0kkw0'><span id='0kkw0'><b id='0kkw0'><form id='0kkw0'><ins id='0kkw0'></ins><ul id='0kkw0'></ul><sub id='0kkw0'></sub></form><legend id='0kkw0'></legend><bdo id='0kkw0'><pre id='0kkw0'><center id='0kkw0'></center></pre></bdo></b><th id='0kkw0'></th></span></q></dt></tr></i><div id='0kkw0'><tfoot id='0kkw0'></tfoot><dl id='0kkw0'><fieldset id='0kkw0'></fieldset></dl></div>

        <tfoot id='0kkw0'></tfoot>

        在连接上选择不同的记录

        Select distinct records on a join(在连接上选择不同的记录)
        • <bdo id='rwcJW'></bdo><ul id='rwcJW'></ul>
        • <tfoot id='rwcJW'></tfoot>

          <legend id='rwcJW'><style id='rwcJW'><dir id='rwcJW'><q id='rwcJW'></q></dir></style></legend>

              <tbody id='rwcJW'></tbody>

            <i id='rwcJW'><tr id='rwcJW'><dt id='rwcJW'><q id='rwcJW'><span id='rwcJW'><b id='rwcJW'><form id='rwcJW'><ins id='rwcJW'></ins><ul id='rwcJW'></ul><sub id='rwcJW'></sub></form><legend id='rwcJW'></legend><bdo id='rwcJW'><pre id='rwcJW'><center id='rwcJW'></center></pre></bdo></b><th id='rwcJW'></th></span></q></dt></tr></i><div id='rwcJW'><tfoot id='rwcJW'></tfoot><dl id='rwcJW'><fieldset id='rwcJW'></fieldset></dl></div>

            <small id='rwcJW'></small><noframes id='rwcJW'>

                  本文介绍了在连接上选择不同的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有两个 mysql 表 - 一个销售表:

                  I have two mysql tables - a sales table:

                  +----------------+------------------------------+------+-----+---------+-------+
                  | Field          | Type                         | Null | Key | Default | Extra |
                  +----------------+------------------------------+------+-----+---------+-------+
                  | StoreId        | bigint(20) unsigned          | NO   | PRI | NULL    |       |
                  | ItemId         | bigint(20) unsigned          | NO   |     | NULL    |       |
                  | SaleWeek       | int(10) unsigned             | NO   | PRI | NULL    |       |
                  +----------------+------------------------------+------+-----+---------+-------+
                  

                  和一个项目表:

                  +--------------------+------------------------------+------+-----+---------+-------+
                  | Field              | Type                         | Null | Key | Default | Extra |
                  +--------------------+------------------------------+------+-----+---------+-------+
                  | ItemId             | bigint(20) unsigned          | NO   | PRI | NULL    |       |
                  | ItemName           | varchar(100)                 | NO   |     | NULL    |       |
                  +--------------------+------------------------------+------+-----+---------+-------+
                  

                  销售表包含每个 ItemID 的多个记录 - 每个 SaleWeek 一个.我想通过加入两个表格来选择所有出售的商品,如下所示:

                  The sales table contains multiple records for each ItemID - one for each SaleWeek. I want to select all items sold by joining the two tables like so:

                  SELECT items.ItemName, items.ItemId FROM items
                  JOIN sales ON items.ItemId = sales.ItemId 
                  WHERE sales.StoreID = ? ORDER BY sales.SaleWeek DESC;
                  

                  但是,这会根据每个 SaleWeek 的多个条目返回多个 ItemId 值.我可以做一个不同的选择来只返回一个 ItemID - 我不想查询最新的 SaleWeek,因为有些项目可能没有最新的条目SaleWeek 所以我需要获得最后一次销售.我是否需要指定 DISTINCT 或使用 LEFT OUTER JOIN 或其他什么?

                  However, this is returning multiple ItemId values based on the multiple entries for each SaleWeek. Can I do a distinct select to only return one ItemID - I don't want to have to query for the latest SaleWeek because some items may not have an entry for the latest SaleWeek so I need to get the last sale. Do I need to specify DISTINCT or use a LEFT OUTER JOIN or something?

                  推荐答案

                  DISTINCT 应该可以满足您的需求:

                  A DISTINCT should do what you're looking for:

                  SELECT DISTINCT items.ItemName, items.ItemId FROM items
                  JOIN sales ON items.ItemId = sales.ItemId 
                  WHERE sales.StoreID = ? ORDER BY sales.SaleWeek DESC;
                  

                  那只会返回不同的 items.ItemName, items.ItemId 元组.

                  That would return only distinct items.ItemName, items.ItemId tuples.

                  这篇关于在连接上选择不同的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to update a record using sequelize for node?(如何使用节点的 sequelize 更新记录?)
                  How to provide a mysql database connection in single file in nodejs(如何在 nodejs 中的单个文件中提供 mysql 数据库连接)
                  Looping Over Result Sets in MySQL(在 MySQL 中循环结果集)
                  How to get Top 5 records in SqLite?(如何获得 Sqlite 中的前 5 条记录?)
                  FULL OUTER JOIN with SQLite(使用 SQLite 进行完全外部联接)
                  SQL Server SELECT where any column contains #39;x#39;(SQL Server SELECT,其中任何列包含“x)
                    <bdo id='QDkeg'></bdo><ul id='QDkeg'></ul>
                  • <tfoot id='QDkeg'></tfoot>

                        <i id='QDkeg'><tr id='QDkeg'><dt id='QDkeg'><q id='QDkeg'><span id='QDkeg'><b id='QDkeg'><form id='QDkeg'><ins id='QDkeg'></ins><ul id='QDkeg'></ul><sub id='QDkeg'></sub></form><legend id='QDkeg'></legend><bdo id='QDkeg'><pre id='QDkeg'><center id='QDkeg'></center></pre></bdo></b><th id='QDkeg'></th></span></q></dt></tr></i><div id='QDkeg'><tfoot id='QDkeg'></tfoot><dl id='QDkeg'><fieldset id='QDkeg'></fieldset></dl></div>
                        <legend id='QDkeg'><style id='QDkeg'><dir id='QDkeg'><q id='QDkeg'></q></dir></style></legend>
                            <tbody id='QDkeg'></tbody>

                          1. <small id='QDkeg'></small><noframes id='QDkeg'>