MYSQL 查询问题搜索

发布于 2024-12-10 12:45:40 字数 613 浏览 1 评论 0原文

已经读了几天了,似乎无法解决我的问题。

让我深入了解我的数据库:

table1代码:

id   CODE
1    D0G08H13L12
2    D1G12H10L12
3    D0G10H12L11
4    D1G10H10L09
5    D0G08H13L12
6    D1G12H10L12
7    D0G08H13L12
8    D1G10H10L09
9    D0G08H13L12
10   D1G12H10L12

现在我正在搜索一个查询来计算在D0G08H13L12之后出现次数最多的代码,因此在下一条记录中搜索我的查询答案将是:

3 times  D1G12H10L12
1 time   D1G10H10L09

这就是我所拥有的到目前为止,我认为我必须使用子查询,但我不太确定。

SELECT id, code, COUNT(code) FROM table1 WHERE id IN ($idprint +1) GROUP BY code 
ORDER BY COUNT(code) DESC

你们中的任何 DB 专业人士都可以帮助我朝好的方向发展吗?

Have been reading for a few days now and can't seem to get hold of my problem here.

Let me give you an insight of my DB:

table1 codes:

id   CODE
1    D0G08H13L12
2    D1G12H10L12
3    D0G10H12L11
4    D1G10H10L09
5    D0G08H13L12
6    D1G12H10L12
7    D0G08H13L12
8    D1G10H10L09
9    D0G08H13L12
10   D1G12H10L12

now I'm searching for a query to count which codes occurs the most after D0G08H13L12, so searching in the next record my answer from the query would be:

3 times  D1G12H10L12
1 time   D1G10H10L09

this is what I have until now, I think I have to use subquery but I'm not quite sure.

SELECT id, code, COUNT(code) FROM table1 WHERE id IN ($idprint +1) GROUP BY code 
ORDER BY COUNT(code) DESC

can any of you DB pro's help me in the good direction?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜嗑 2024-12-17 12:45:40

试试这个:

SELECT COUNT(id) tot, `code`
FROM your_table
GROUP BY `code`
HAVING tot <= 
    (SELECT COUNT(id) FROM your_table
     WHERE code = 'D0G08H13L12')
    AND code <> 'D0G08H13L12'
ORDER BY tot DESC

如果您只想有两条记录,请将其附加到查询中:

LIMIT 2

Try this:

SELECT COUNT(id) tot, `code`
FROM your_table
GROUP BY `code`
HAVING tot <= 
    (SELECT COUNT(id) FROM your_table
     WHERE code = 'D0G08H13L12')
    AND code <> 'D0G08H13L12'
ORDER BY tot DESC

If you want to have only two records append this to the query:

LIMIT 2
无风消散 2024-12-17 12:45:40

试试这个,这会给你想要的结果......

    Select id, code, count(code) 
from table1 
where id>(select max(id) from table1 where code =D0G08H13L12) group by code order by count(code) desc;

Try this, This will give u the desired result....

    Select id, code, count(code) 
from table1 
where id>(select max(id) from table1 where code =D0G08H13L12) group by code order by count(code) desc;
青衫儰鉨ミ守葔 2024-12-17 12:45:40
select min(x.id), x.code
from yourTable x,
  (
  select * from yourTable
  where code = 'D0G08H13L12'
  ) v
where
  x.id > v.id
group by v.id

获取输入的最多下一个代码。

select min(x.id), x.code
from yourTable x,
  (
  select * from yourTable
  where code = 'D0G08H13L12'
  ) v
where
  x.id > v.id
group by v.id

To get the most next codes for entered one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文