简体   繁体   English

MySQL查询从另一个表中获取单个项目

[英]MySQL Query to get individual item from another table

I have two tables- 1. Feed 2. Seller 我有两个表-1. Feed 2. Seller

Feed table is like this Feed表是这样的

seller_id       item_id     end_date
=========       =======     =========
1               1           2017-02-12[A future date]
1               2           2017-03-12[A future date]
1               3           2017-01-12[A past date]
2               12          2017-02-12[A future date]
3               123         2017-01-10[A past date]

Seller table is like this 卖方表是这样的

seller_id
=========
1
2
3

I like to get the result like this where end_date is greater than or equal to Current Date and I need only a single item_id for each seller_id- 我喜欢这样的结果,其中end_date大于或等于“当前日期”,并且对于每个Seller_id,我只需要一个item_id-

seller_id       item_id
=========       =======
1               1 or 2
2               12

And my SQL Query is like- 我的SQL查询就像-

SELECT S.seller_id, R.item_id FROM Feed F, Seller S WHERE S.seller_id=F.seller_id AND F.end_date>=CURDATE() GROUP BY S.seller_id LIMIT 1;

Check this.. 检查一下..

SELECT S.seller_id, MAX(R.item_id) AS item_id 
    FROM Feed F, Seller S 
    WHERE S.seller_id=F.seller_id 
    AND F.end_date>=CURDATE() 
    GROUP BY S.seller_id;

You can use min() , or max() , or even any_value() aggregate function with a group by clause to select a item_id per seller: 您可以将min()max()甚至any_value()聚合函数与group by子句一起使用, item_id每个卖方选择一个item_id

SELECT S.seller_id, max(R.item_id)
FROM Feed F
INNER JOIN Seller S on S.seller_id=F.seller_id
WHERE F.end_date>=CURDATE()
GROUP BY S.seller_id

It depends on your exact business requirements which function to use. 这取决于您要使用的功能的确切业务要求。

You only need the table Feed, you will group by id_seller after having selected items with enddate greater than current date. 您只需要表Feed,在选择结束日期大于当前日期的项目后,将按id_seller分组。 Then select a unique item_id by some aggregate function of your choice ( max() here). 然后通过您选择的某些聚合函数(此处为max())选择唯一的item_id。 Here is an exemple of your request : 这是您要求的一个例子:

SELECT
    F.seller_id,
    max(F.itemid)
FROM
    Feed F
WHERE
    F.end_date > CURRENT_DATE

GROUP BY
    F.seller_id

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

相关问题 MySQL 查询从一个表中获取一个项目从另一个表中获取多个项目 - MySQL query to get one item from a table an multiple items from another table mysql:从另一个查询获取表名 - mysql: get table names from another query MySQL从查询中的另一个表获取值 - MySQL get value from another table in query MySQL查询以获取表2中的总项目数到表1中的所有者? - MySQL query to get total item number from table 2 to owner in table 1? MYSQL查询 - 使用另一个表中的几个ID来汇总项目 - MYSQL Query - sum item using several ids from another table Sql 查询以从另一个表中获取包含多个 id 的项目 - Sql Query to get an item that contains multiple ids from another table 需要从主表获取订单的MySQL查询,然后是与之关联的单个项目以及另一个表的总行数的计数 - Need MySQL query that gets orders from main table, then a count of individual items associated with it as well as total rows from another table MySQL查询从另一个表的值列表中获取值 - Mysql query to get value from list of values in another table MYSQL PHP查询将数据从一个表获取到另一个表 - MYSQL PHP query to get data from one table to another MySQL查询(或Doctrine 1.2查询)-从联接表和过滤器中获取最新项目 - MySQL Query (or Doctrine 1.2 query) - get most recent item from joined table and filter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM