0

I have 1 main table and two tables that hold multiple dinamyc information about the first table.

The first table called 'items' holds main information. Then there are two tables (ratings and indexes) that holds information about some values for dinamyc count of auditories and time period.

What i want: When I query for those items, I want result to have an additional column names from ratings and indexes tables.

I have the code like this

SELECT items.*, ratings.val AS rating, indexes.val AS idx
FROM items,ratings,indexes 
WHERE items.date>=1349902800000 AND items.date <=1349989199000 
AND ratings.period_start <= items.date 
AND ratings.period_end > items.date 
AND ratings.auditory = 'kids'
AND indexes.period_start <= items.date 
AND indexes.period_end > items.date 
AND indexes.auditory = 'kids'
ORDER BY indexes.added, ratings.added DESC

The tables look something like this

items:

`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(200) DEFAULT NULL,
`date` bigint(40) DEFAULT NULL
PRIMARY KEY (`id`)

ratings:

`id` bigint(50) NOT NULL AUTO_INCREMENT,
`period_start` bigint(50) DEFAULT NULL,
`period_end` bigint(50) DEFAULT NULL,
`val` float DEFAULT NULL,
`auditory` varchar(200) DEFAULT NULL,
`added` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)

All dates except 'added' fields which are simple TIMESTAMPS are BIGINT format - miliseconds from whatever date it is in AS3 when you do Date.getTime();

So - what is the correct way to get this acomplished?

4

1 回答 1

0

我唯一没有看到的是任何单个项目与其评级的独特相关性......我认为评级表需要一个“ItemID”来链接回项目。就目前而言,如果您在给定的时间段内有 100 个项目,比如 3 个月......并且只需添加所有评级/评论,但不要将这些评级与实际项目相关联,那么您就会陷入困境。将 ItemID 放入并将其添加到您的 WHERE 条件中,您应该一切顺利。

于 2013-08-07T15:35:56.017 回答