1

假设我有一个数据库表uploads,它具有id、user_id、upload、createdDate和一个反映用户表的实体类。

然后我有一个类似的查询。

SELECT u.id, 
  u.name, 
  u.createdDate,
  u.user_id,
  (SELECT COUNT(*) 
   FROM uploads
   WHERE user_id in 
       (SELECT id from uploads
        WHERE user_id = :id) 
    AND createdDate BETWEEN date_sub(NOW(),interval 1 day) 
    AND date_sub(NOW(),interval 0 day)) as countLastDayUploads
FROM uploads as u
WHERE u.user_id = :id

这将返回一个额外的值countLastDayUploads,它不会反映在 schema 中。 这里有两个问题。 countLastDayUploads 是否应该存储在没有注释的上传实体类中?我想是的...

其次,我将如何使用 Symfony2 学说查询构建器编写此查询。

4

2 回答 2

0
SELECT u.id, 
  u.name, 
  u.createdDate,
  u.user_id,
  count(*) as count
FROM uploads as u
WHERE 
    u.user_id = :id
  AND 
    createdDate > (NOW() - INTERVAL 1 DAY)
GROUP BY u.user_id

如果我理解你的正确 - 此代码必须工作五

id, user_id, upload, createdDate- 错误的名字

id, userId, upload, createdDate- 更好,但错误

id, user_id, upload, created_date- 好

这是mysql的标准。

于 2012-10-30T15:15:26.610 回答
0

您应该将其添加到省略注释的实体中。教义应该将结果映射到对象。

于 2012-10-30T22:31:14.957 回答