Mysql查询问题
我的 mysql 查询有问题。
我的数据库:
sections
id
section_name
grades
id
user_id
section_id
date
grade
我希望我的数据显示如下:
Section_name 成绩
但是我希望成绩最接近今天的日期... 这是我到目前为止所拥有的,但它没有显示最新的成绩。相反,它按 id 排序(我猜)
SELECT *
FROM
grades,
sections
WHERE
sections.id = grades.section_id
AND
grades.user_id = '.$id.'
GROUP BY grades.section_id
ORDER BY grades.date DESC
编辑:$id 变量是会话中的用户 id。
I have a problem with my mysql query.
My database:
sections
id
section_name
grades
id
user_id
section_id
date
grade
I want my data to be shown like this:
Section_name grade
But i wat want the grade to be the closest to todays date...
This is what i have so far but it doesn't show the latest grade. instead it orders by id (I guess)
SELECT *
FROM
grades,
sections
WHERE
sections.id = grades.section_id
AND
grades.user_id = '.$id.'
GROUP BY grades.section_id
ORDER BY grades.date DESC
EDIT: the $id variable is the user id from a session.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我的解决方案涉及使用子查询来获取最新成绩的 id,然后将其传递给查询,然后查询连接部分和成绩表以返回部分名称和成绩。请将此更多地视为伪代码而不是有效的 SQL 查询,因为我没有时间测试它。稍后可能会回来编辑它
SELECTsection_name,gradeFROMsections,gradesWHEREsections.id=grades.idANDgrades.id=(SELECTidFROMgradesWHEREsection_id='$Id'ORDERbydateDESCLIMIT1)
My solution involves the use of subquery to get the id of the latest grade and then passing it up to the query which then joins both sections and grades table to return section_name and grade. Please consider this more as a psuedocode than a valid sql query as I dont have time to test it. Might come back later to edit it
SELECT section_name, grade FROM sections, grades WHERE sections.id = grades.id AND grades.id = (SELECT id FROM grades WHERE section_id = '$Id' ORDER by date DESC LIMIT 1)
我会根据您想要的特定 user_id 进行预查询,并找到每个部分的最大日期。然后,重新加入回部分和成绩(现在此预查询将极大地限制要加入的结果集)。然后获取部分名称,最后获取与参加课程的特定学生(用户)匹配的日期的正确成绩。
I would pre-query based on the specific user_id you wanted, and find their max date per section.. Then, re-join back to sections and grades (now that this prequery will extremely limit the result set to join against). Then get section name and finally proper grade for the date matching the specific student (user) taking the course.
试试这个?
try this?
我最近不得不做一些类似的事情,但是使用 T-SQL,所以你必须自己更改它才能使其在 MySQL 中工作(我在这里没有安装,所以恐怕我无法为你检查)。
添加到 WHERE 子句:
GETDATE()
相当于NOW()
,TOP 1
相当于LIMIT 1
以及其他函数,我不知道 MySQL 的等价函数!I recently had to do something similiar, but with T-SQL so you'll have to change it yourself to get it to work in MySQL (which I don't have installed here so I can't check for you I'm afraid).
Add to the WHERE clause:
GETDATE()
is equivalent toNOW()
,TOP 1
is likeLIMIT 1
and the other functions, well I don't know the MySQL equivalents off the top of my head!你可以使用它,它可以 100% 工作
you can use this it is working 100%
如果我答对了,你想要每个部分的最新成绩。这不能简单地用 MySQL 来完成。
您需要一个窗口函数或一个序列生成器,这两者在 MySQL 中都不可用。然而,有一个使用变量的解决方法。请参阅此问题了解类似的用例:
MySQL 查询:使用 UNION 并获取行号作为 SELECT 的一部分
此处的其他背景信息:
SQL/mysql - 选择不同/唯一但返回所有列?
您最终可能会做的是一个大的嵌套查询,如下所示:
If I get this right you want the latest grade per section. This cannot be done simply with MySQL.
You'd need a window function or a sequence generator, neither of which are available in MySQL. There is a workaround using variables, however. See this question for a similar use-case:
MySQL query: Using UNION and getting row number as part of SELECT
Additional background information here:
SQL/mysql - Select distinct/UNIQUE but return all columns?
What you'll likely end up doing is a big nested query that will look like this:
如果我理解正确的话:
If I understand correctly:
假设我们正在寻找 id=1 的用户的等级(只是为了测试没有 php 的查询)。
您可以稍后将其替换为“$Id”。
如果您的日期列足够精确,以确保它对于您可以使用的每个成绩都是唯一的:
如果您的日期不是唯一的,那么您必须根据依赖子查询中找到的 id 将成绩连接回自身:
您需要索引 (user_id,section_id,日期)用于此查询。
@评论:
好的,我将尝试解释第二个查询,因为它在任何情况下都会给出正确的结果。
在表 g1 中,我们从 id=1 用户的成绩中获取行,我们按部分对它们进行分组,并且在每个部分中我们找到一个最大日期 - 这意味着最近的日期。此时,我们还不知道哪一行准确地保存了这个最新日期,因为我们只能选择
group by
或聚合函数(如 max())中的列。 Mysql 允许选择其他列,例如年级,称为隐藏列(其他数据库只会抛出语法错误),但可以自由地返回每个组中的任何行,通常不是我们想要的行,并且我们想要保存最新的行日期。如果所有行都具有相同的值,例如本例中的 user_id ,那就可以了,但是我们需要成绩,每个组中的成绩可能不同。对于小表, select 可以返回正确的表,这就是为什么有些人声称按日期排序可以提供帮助,因为他们在小表上进行测试并看到正确的结果,但是一旦表增长,有行更新、删除等,它就会出错在。基本上我们现在有一个部分和最近日期的列表,我们需要找出成绩。因此,我们必须加入这个 g1 表,我们刚刚进入了 Grades 表,以找到包含每个部分的最新日期的行。 Id 是唯一我们确定是唯一的列(如果我们不加入唯一列或唯一列列表,我们将获得更多行,并且我们只需要一行),因此我们尝试在依赖子查询 g2 中找到此 id (它是一个子查询,引用来自外部的值,在本例中是来自 gmax,它只是 g1 的别名,如前所述)。
一旦我们获得了每个section_id的成绩,剩下要做的唯一一件事就是将其连接到section_id上的section表以获取section_name而不是其id值。
Lets assume we are looking for grades of user with id=1 (just to test query without php).
You can replace it with '$Id' later.
If your date column is precise enough to ensure it is unique for every grade you can use:
If your date is not unique then you have to join grades back to itself on id found in dependent subquery:
You need index on (user_id, section_id, date) for this query.
@comment:
Ok I'll try to explain the second query as it gives correct results for any case.
In table g1 we take rows from grades for user with id=1, we group them by section and in every section we find a maximum date - which means most recent date. At this point we don't know yet which row exactly holds this most recent date, because we can select only columns that are in the
group by
or aggregate functions (like max()). Mysql allows to select other columns, like grade, called hidden columns (other dbs would just throw a syntax error), but is free to return any row from each group, usually not the one we want, and we want the one holding most recent date. If all rows have the same value, like user_id in this case, its ok, but we need grades, which can be different in every group. For small tables select can return the right one, thats why some people claim ordering by date can help, because they test it on small tables and see correct results, but it goes wrong once the table grows, there are row updates, deletes and so on.Basically we now have a list of sections and most recent dates and we need to find out grades. So we have to join this g1 table we just got to the grades table to find a row that holds the most recent date for every section. Id is the only column we are sure to be unique (if we don't join on unique column or unique list of columns we'll get more rows and we want exactly one) so we try to find this id in dependent subquery g2 (its a subquery that references values from outside, from gmax in this case, which is just an alias for g1, explained earlier).
Once we have grade for every section_id the only thing left to do is to join that to sections table on section_id to get section_name instead of its id value.