我有一个表(MYSQL)'product',其中一个名为'category_id'的列存储来自另一个表'category'的类别id。因此'category_id'列中有多个类别。例如下表。现在我想从每个类别(例如2)中选择固定数量的产品,例如我想从产品表中选择每个类别的2个最新产品。我应该使用什么mysql查询,任何想法?
产品:
+-------------+------+---------------------+
| category_id | name | timestamp |
+-------------+------+---------------------+
| 1 | abc | 2016-02-27 16:04:00 |
| 1 | def | 2016-02-28 16:10:00 |
| 1 | ghi | 2016-02-29 16:11:00 |
| 2 | pqr | 2016-02-27 16:04:00 |
| 2 | stu | 2016-02-27 16:05:00 |
| 3 | vwx | 2016-02-28 16:04:00 |
+-------------+------+---------------------+
预期结果:
产品:
+-------------+------+---------------------+
| category_id | name | timestamp |
+-------------+------+---------------------+
| 1 | def | 2016-02-28 16:10:00 |
| 1 | ghi | 2016-02-29 16:11:00 |
| 2 | pqr | 2016-02-27 16:04:00 |
| 2 | stu | 2016-02-27 16:05:00 |
| 3 | vwx | 2016-02-28 16:04:00 |
+-------------+------+---------------------+
答案 0 :(得分:1)
它可能对你有帮助......
检查SQL小提琴链接中的输出:http://sqlfiddle.com/#!9/3942d2/6
SELECT
category_id,
name,
tsDate
FROM
(
SELECT
category_id,
name,
tsDate,
@rn := IF(@prev = category_id, @rn + 1, 1) AS rn,
@prev := category_id
FROM Product
JOIN (SELECT @prev := NULL, @rn := 0) AS vars
ORDER BY category_id, tsDate DESC, name
) AS T1
WHERE rn <= 2
答案 1 :(得分:0)
试试这个:
File theFile=new File(selectedPath);
Wini ini=new Wini(theFile);
System.out.println("path== "+selectedPath);
String field = "soc.gif" + ",0";
ini.put(".ShellClassInfo", "IconResource", field);
ini.store();
System.out.println("path2== "+selectedPath);
Runtime.getRuntime().exec("attrib -h +s " + theFile.getAbsolutePath());