- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
对于给定的类别,我试图找到 Track_Record 中记录的不同位置最多的产品 item_name。
无论我尝试什么,我似乎都无法弄清楚这个查询。
如果有人不介意给我一些提示,我将不胜感激。
这些是相关表格:
CREATE TABLE IF NOT EXISTS `Track_Record` (
`longitude` varchar(15) NOT NULL ,
`lattitude` varchar(15) NOT NULL ,
`datetime` DATETIME NOT NULL,
`EPC` varchar(200) NOT NULL ,
`ip` varchar(50) NOT NULL ,
PRIMARY KEY (ip, EPC, datetime),
FOREIGN KEY (EPC) REFERENCES Product(EPC) ON DELETE CASCADE,
FOREIGN KEY (ip) REFERENCES RFID_Reader(ip) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS `Prod_Cat` (
`category_id` int(20) NOT NULL ,
`EPC` varchar(200) NOT NULL ,
PRIMARY KEY (category_id, EPC),
FOREIGN KEY (EPC) REFERENCES Product(EPC) ON DELETE CASCADE,
FOREIGN KEY (category_id) REFERENCES Category(category_id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS `Product` (
`EPC` varchar(200) NOT NULL ,
`expiry_date` DATE NOT NULL,
`production_date` DATE NOT NULL,
`prod_description` varchar(200) NOT NULL ,
`item_name` varchar(100) NOT NULL ,
`manufacturer_info` varchar(200) NOT NULL ,
`account_name` varchar(100) NOT NULL ,
`password` varchar(100) NOT NULL ,
PRIMARY KEY (EPC),
FOREIGN KEY (password, account_name) REFERENCES Owner(password, account_name) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS `Category` (
`category_id` int(20) NOT NULL,
`name` varchar(100) NOT NULL ,
`description` varchar(200) NOT NULL,
`sub_category_id` int(20) NULL,
PRIMARY KEY (category_id),
FOREIGN KEY Category(sub_category_id) REFERENCES Category(category_id) ON DELETE CASCADE
);
这是数据:
Category (name, description, category_id)
VALUES
('Technology', 'Technological Devices', 0),
('Dining Wear', 'Dinner plates and cups etc.', 1),
('Clothing', 'Stuff you wear', 2),
('Stationary', 'Office stuff!', 3);
Product (EPC, expiry_date, production_date, prod_description, item_name, manufacturer_info, password, account_name)
VALUES
('01.0000A89.00016F.000169DCD', '2019-01-09','2014-01-09', 'for eating off', 'Plate', 'man@company.com', 'a', 'John Smith'),
('02.0000A89.00016F.000169DCD', '2016-07-03','2013-01-23', 'for drinking from', 'Cup', 'somebody@aplaceinspace.com', 'c', 'John Mccane Manufacturer'),
('03.0000A89.00016F.000169DCD', '2018-09-23','2012-02-09', 'For playing games', 'Playstaion', 'nerdsrus@bamboozled.com', 'f', 'John Twitching'),
('04.0000A89.00016F.000169DCD', '2015-02-12','2014-03-11', 'For playing CDs', 'CD Player', 'whynotuseMP3@hopeless.com', 'd', 'John Dickson Retailer'),
('05.0000A89.00016F.000169DCD', '2017-06-11','2014-06-02', 'USB Storage Device', 'USB Stick', 'imustbeenglish@pommebugger.com', 'b', 'John Mitchal'),
('06.0000A89.00016F.000169DCD', '2019-02-08','2014-08-04', 'Wrap around your neck as a fashion statement', 'Boa Constrictor', 'byebye@shortofbreath.com', 'c', 'John Mccane Manufacturer');
Prod_Cat (category_id, EPC)
VALUES
('0', '03.0000A89.00016F.000169DCD'),
('0', '04.0000A89.00016F.000169DCD'),
('0', '05.0000A89.00016F.000169DCD'),
('1', '01.0000A89.00016F.000169DCD'),
('1', '02.0000A89.00016F.000169DCD'),
('2', '06.0000A89.00016F.000169DCD');
Track_Record (ip, longitude, lattitude, datetime, EPC)
VALUES
('000.111.222', '27.4667 S', '153.0333 E', '2014-11-05 18:56:46', '03.0000A89.00016F.000169DCD'),
('000.111.222', '27.4667 S', '153.0333 E', '2015-05-12 13:21:16', '03.0000A89.00016F.000169DCD'),
('555.666.777', '22.2783 N', '114.1747 E', '2012-07-19 12:22:16', '04.0000A89.00016F.000169DCD'),
('000.111.222', '27.4667 S', '153.0333 E', '2011-03-01 11:43:26', '03.0000A89.00016F.000169DCD'),
('555.666.777', '22.2783 N', '114.1747 E', '2014-09-02 18:53:14', '06.0000A89.00016F.000169DCD'),
('222.333.444', '59.3500 N', '18.0667 E', '2015-10-15 18:23:18', '04.0000A89.00016F.000169DCD'),
('333.444.555', '15.7833 S', '47.8667 W', '2015-02-22 19:53:16', '01.0000A89.00016F.000169DCD'),
('444.555.666', '51.5072 N', '0.1275 W', '2013-01-11 22:21:15', '04.0000A89.00016F.000169DCD');
最佳答案
获取计数是一个简单的聚合:
select pc.cat_id, p.item_name, count(*)
from track_record tr join
prod_category pc
on tr.epc = pc.epc join
product p
on p.epc = pc.epc
group by pc.cat_id, p.item_name;
获取最大计数的最简单方法可能是使用 substring_index()
/group_concat()
技巧:
select pc.cat_id, cnt,
substring_index(group_concat(item_name order by cnt desc), ',', 1) as most_common_item
from (select pc.cat_id, p.item_name, count(*) as cnt
from track_record tr join
prod_category pc
on tr.epc = pc.epc join
product p
on p.epc = pc.epc
group by pc.cat_id, p.item_name
) x
group by pc.cat_id;
关于mysql - 如何选择第二个表中具有最多不同行的行 - MySQL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30438251/
看来我又被一个简单的正则表达式卡住了。 我想要什么: 1 到 999 之间的数字 可选:逗号、符号 如果输入逗号,最少1位小数,最多3位小数点应该是presebt。 Allowed: 100 999,
我需要从两列中获取最大值并将其发送到第三列。这些列的大小都是统一的,但有时大小会有所不同,但它们都将从同一个单元格开始。例如: 5 8 - 6 2 - 6 5 - 带有破折号的列需要找到其他两个之间的
我在我的网站上有一张包含用户排名列的表格,排名是一个数字,我想选择排名最高的 3 个用户,所以我查看并搜索了我认为最好的查询是那:Link (正确答案的第二个查询),但我不明白查询,如果有人能一步一步
我正在尝试制作一个点击计数器,我想收集 24 小时内的总点击次数。无论最终用户位于哪个时区,这 24 小时都应该是固定值。在 24 小时内,数据库应更新为 +1 次点击计数,一旦达到 24 小时时间范
我有一个在典型共享主机上运行的 PHP + MySQL Web 应用程序,我想知道调用最多的查询是什么以及消耗的资源量是多少。这样,我将专注于最昂贵的查询以优化资源或检测优化不佳的查询。 例如: qu
这是我“尝试”从用户输入的数字中找到最大 2 个值的代码: #include using namespace std; void maximum2(int a, int& max1,int& max
我需要编写一个 Python 函数,从具有最多“o”字符的字符串中返回单词。例如,get_most_ooo_word(['aa ao ooo']) 应该返回 ['ooo'] 和 get_most_oo
我正在寻找一种哈希算法,以创建尽可能接近字符串的唯一哈希值 (max len = 255),从而生成一个长整数 (DWORD)。 我意识到26^255 >> 2^32,但也知道英语的单词数远少于2^3
我得到了一个仅由 's','t','u','v' 作为字符组成的字符串 T。我想找到长度为 |T| 的字符串数它最多与 T 不同 n 个位置。而且每个这样的字符串在三个不同的位置不能有相同的字符,这些
我有一群“专家”(大约 300 人)可以胜任一项工作。而且我有很多工作必须完成,比如说大约 500 个。我也有信息,一个专家能做某项工作有多“好”。这将导致一个 300 x 500 的矩阵来保存权重。
我正在尝试解决这个问题,虽然我可以使用蛮力解决它,但是以下优化算法为我提供了一些测试用例的错误结果。我尝试了但无法找到代码的问题,任何人都可以帮助我。 问题:给定一个字符串 S 和整数 K,找到整数
我需要一个混合长度的正则表达式验证,总长度为 6 个字符,其中 4-6 个大写/数字字符和 0-2 个空格。 我试过 ^[A-Z0-9]{4,6}+[\s]{0,2}$ 但它导致最大长度为 8 个字符
我有一个数组 {-1,2,3,4,-3,-2,1,5} 现在我想找到给定数组的最小连续总和子数组,最多 K 次交换。 在上面的数组中,最小连续和是-5,子数组是{-3,-2} 对于 K=1 我应该如何
我们有一个简单的表格如下: ------------------------------------------------------------------------ | Name |
如果哈希不能超过 4 个字符,并且这 4 个字符只能是小写字母或数字,那么创建 String 哈希的最佳方法是什么? 我要散列的字符串有 1-255 个字符。我知道在没有冲突的情况下创建 4-char
我希望使用 Multipeer Connectivity 框架,并感谢任何关于如何最好地进行的经验之谈。 我需要在“教练”设备和最多 45 个“玩家”设备之间建立连接。他们都在同一个空间,但无法预测
给定一个数组 a,什么是实现其组合直到第 n 的最佳方法?例如: a = %i[a b c] n = 2 # Expected => [[], [:a], [:b], [:c], [:a, b], [
这个问题在这里已经有了答案: Formatting floats without trailing zeros (21 个回答) 关闭8年前。 我想格式化最多包含 2 个小数位的 float 列表。但
我无法使用以下形式的命令登录到远程 docker 注册表: docker login –u my-username –p my-password registry.myclient.com 我得到的错
所以这是我的代码:服务器.java import java.io.*; import java.net.*; import java.util.*; class Server implements R
我是一名优秀的程序员,十分优秀!