简体   繁体   English

数据库查询的 PHP 和 MySQL 问题

[英]PHP and MySQL Issues with database query

I have a MySQL table with userId, movieId and date.我有一个包含 userId、movieId 和 date 的 MySQL 表。 I'm having issues with my query.我的查询有问题。 I would like to query and list out different date tagged with a movieId under each userId.我想查询并列出每个 userId 下标有 movieId 的不同日期。


userId movieId  date
  1     A     1:00PM
  1     A     3:00PM
  2     A     2:00PM
  2     C     8:00PM
  2     C     9:00PM

I want something like this我想要这样的东西


UserId --Movie(Time)
  1    A     1:00PM, 3:00PM
  2    A     2:00PM
  2    C     8:00PM,9:00PM

use group_concat function 使用group_concat函数

select UserId ,movieId, group_concat(date separator ',') 
from your_table group by UserId ,movieId 

EDIT: I don't have enough reputation to comment on the accepted answer above but it WILL break on later versions of MySQL and I don't think it's what the poster actually wants - it will not produce the output they said they wanted. 编辑:我没有足够的声誉来评论上面接受的答案,但是它将破坏MySQL的更高版本,我认为这不是发布者真正想要的-它不会产生他们所说的想要的输出。

You need to use GROUP BY to collapse several rows into one. 您需要使用GROUP BY将几行折叠为一。 And then you want to use an aggregating function to join the data in those rows. 然后,您想使用聚合功能将这些行中的数据连接起来。 An aggregate function is one that takes several rows as input and does something with them to return a single value. 聚合函数是一种将多行作为输入并对其进行处理以返回单个值的函数。 In this case you want group_concat. 在这种情况下,您需要group_concat。

HOWEVER, you need to tell MySQL what to do with EVERY SINGLE ELEMENT you select from the results if you use GROUP BY. 但是,如果使用GROUP BY,则需要告诉MySQL如何处理从结果中选择的每个单元素。 This is because in ANSI SQL a statement must be deterministic. 这是因为在ANSI SQL中,语句必须是确定性的。 Ie return the same output every time for the same input. 即每次为相同的输入返回相同的输出。 Older versions of MySQL would 'guess' values for you but newer ones (5.7.5 and after) by default wont. 较旧的MySQL版本会为您“猜测”值,但默认情况下,较新的版本(5.7.5及更高版本)不会。 Also, it's good practice to explicitly tell MySQL how to handle each value anyway. 另外,最好还是明确告诉MySQL如何处理每个值。 So you want something like this: 所以你想要这样的东西:

SELECT userId, ANY_VALUE(movieId), GROUP_CONCAT(date SEPARATOR ", ") 
FROM tableName GROUP BY UserId;

The above will give you each user id and the list of dates. 上面将为您提供每个用户ID和日期列表。 But the movie code will be randomly selected. 但是电影代码将被随机选择。

OR you want something like this: 或者您想要这样的东西:

SELECT userId, GROUP_CONCAT(movieId SEPARATOR ", "), GROUP_CONCAT(date SEPARATOR ", ")
FROM tableName GROUP BY UserId;

This will give you the same as before but it will list all the movie codes instead of randomly selecting any value. 这将为您提供与以前相同的功能,但是它将列出所有影片代码,而不是随机选择任何值。

OR you want to group by userId AND movieID so that you can have a row for each user and movie combination. 或者,您想按userId和movieID分组,以便可以为每个用户和电影组合使用一行。 Then you would change the GROUP BY clause at the end, like this: 然后,您将在最后更改GROUP BY子句,如下所示:

SELECT userId, movieId, GROUP_CONCAT(date SEPARATOR ", ")
FROM tableName GROUP BY UserId, movieId;

I've included three different options because I think you may actually want the third one even though you asked for the first. 我提供了三个不同的选项,因为我认为即使您要求第一个,您实际上也可能想要第三个。

MySQL version matters. MySQL版本很重要。 If you are before MySQL 5.7 then the AnyValue(…) function doesn't exist and you should simply remove it and put "movieId" by itself. 如果您使用的是MySQL 5.7之前的版本,那么AnyValue(…)函数将不存在,您只需将其删除,然后单独放置“ movieId”即可。 But if you ware after MySQL 5.7 and also if you want to do things the right way, you should include it. 但是,如果您在MySQL 5.7之后进行购买,并且还想以正确的方式进行操作,则应将其包括在内。

Also, two suggestions. 另外,有两个建议。 Firstly, don't use letters for the movieID unless you have a good reason to. 首先,除非有充分的理由,否则不要将字母用作movieID。 Use integers like you have for the userId and put any information about the movies in a separate table that the movieID is a key for. 使用与用户ID相似的整数,并将有关电影的所有信息放在单独的表中,movieID是键。 Secondly, add a column called "id" to your table and make it the Primary Key. 其次,在表中添加一个名为“ id”的列,并将其设置为主键。 This makes many things a lot easier. 这使许多事情变得容易得多。 You can set the id field to be AUTO_INCREMENT - that's the normal thing to do. 您可以将id字段设置为AUTO_INCREMENT-这是正常的事情。

Hope this helps. 希望这可以帮助。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM