我正在使用mysql作为数据库。
我必须为具有ContentPrvdr
字段的不同值的客户找到明确的最新结果。
我正在使用以下sql查询:
SELECT distinct ContentPrvdr,LocalDatabaseId,Website,BusID,LastUpdated,
UserCat1Rank_Local,UserCat1Count_Local,Citations,PhotoCount,
VideoCount,Cat_Count FROM local_database WHERE CMCustomerID=10
ORDER BY LocalDatabaseId,LastUpdated LIMIT 0,3
找到结果,但它会返回三个ContentPrvdr
具有相同值的结果。但我想要ContentPrvdr
的不同值结果。
以下是测试的样本数据。
LocalDatabaseId CMCustomerID FranchiseName ContentPrvdr BusName ConvBusName KeyWBizName KeyWCat Website LocationNmbr PhoneLoc StreetLoc Cat_Count Description_Local Citations PhotoCount VideoCount UserContent
41 15 2 For 1 Pizza Co bing 2 For 1 Pizza Co 2 FOR 1 PIZZA CO 2 For 1 Pizza Co, Los Angeles, CA Pizza Restaurant, Los Angeles, CA http://st1.map 1 3232699421 3480 E CESAR E CHAVEZ AVENUE 1 0 0 0 0
41 15 2 For 1 Pizza Co bing 2 For 1 Pizza Co 2 FOR 1 PIZZA CO 2 For 1 Pizza Co, Los Angeles, CA Pizza Restaurant, Los Angeles, CA NULL 1 3232699421 3480 E CESAR E CHAVEZ AVENUE 1 0 0 0 0
56 15 2 For 1 Pizza Co. Google 2 For 1 Pizza Co. 2 FOR 1 PIZZA CO 2 For 1 Pizza Co, Los Angeles, CA Pizza Restaurant, Los Angeles, CA Not Specified 1 2137494515 2528 S. FIGUEROA STREET 2 0 3 0 0
56 15 2 For 1 Pizza Co. Google 2 For 1 Pizza Co. 2 FOR 1 PIZZA CO 2 For 1 Pizza Co, Los Angeles, CA Pizza Restaurant, Los Angeles, CA Not Specified 1 2137494515 2528 S. FIGUEROA STREET 2 Fresh N Ho 23 2 1
65 15 2 For 1 Pizza Co Google 2 For 1 Pizza Co 2 FOR 1 PIZZA CO 2 For 1 Pizza Co, Los Angeles, CA Pizza Restaurant, Los Angeles, CA Not Specified 1 3232699421 3480 EAST CESAR E CHAVEZ AVENUE 1 0 0 0 0
65 15 2 For 1 Pizza Co Google 2 For 1 Pizza Co 2 FOR 1 PIZZA CO 2 For 1 Pizza Co, Los Angeles, CA Pizza Restaurant, Los Angeles, CA Not Specified 3232699421 3480 EAST CESAR E CHAVEZ AVENUE 1 25 0 0 1
126 15 2 For 1 Pizza Co yellopages 2 For 1 Pizza Co 2 FOR 1 PIZZA CO 2 For 1 Pizza Co, Los Angeles, CA Pizza Restaurant, Los Angeles, CA http://www.yellow 1 5628610936 5720 IMPERIAL HWY STE Q 2 EATING PLACE 0 0 0 0
126 15 2 For 1 Pizza Co yellopages 2 For 1 Pizza Co 2 FOR 1 PIZZA CO 2 For 1 Pizza Co, Los Angeles, CA Pizza Restaurant, Los Angeles, CA http://www.yello 1 5628610936 5720 IMPERIAL HWY STE Q 2 EATING PLACE 0 0 0 0
请有人帮助我如何获得具有不同ContentPrvdr
值的结果。
提前致谢
答案 0 :(得分:0)
您可能希望执行以下操作:
SELECT *
FROM
(
SELECT ContentPrvdr, LocalDatabaseId, MAX(LastUpdated) AS Updated
FROM local_database
WHERE CMCustomerID = 10
GROUP BY ContentPrvdr, LocalDatabaseId
ORDER BY Updated DESC
) SQ
INNER
JOIN local_database ld
ON SQ.ContentPrvdr = ld.ContentPrvdr
AND SQ.LocalDatabaseId = ld.LocalDatabaseId
AND SQ.Updated = ld.LastUpdated
LIMIT 0, 3
我特意以这种方式构造了查询,并没有列出“SQ”块中列出的所有列,因此可以看到逻辑的核心。您在“SQ”(以及GROUP BY“和”JOIN ON“)中的SELECT语句中放置的列将需要是”local_database“表中的任何一个并唯一标识行
答案 1 :(得分:0)
尝试类似:
SELECT *
FROM (
SELECT
`ContentPrvdr`,
`LocalDatabaseId`,
`LastUpdated`
FROM `local_database` AS `inner`
WHERE `CMCustomerID`=10
ORDER BY `LastUpdated` DESC
) AS `outer`
GROUP BY `ContentPrvdr`
ORDER BY `LocalDatabaseId`, `LastUpdated`
LIMIT 0,3
如果要获取GROUP BY
子句中命名的列的最新值,则需要在子查询中对其进行排序,因为MySQL似乎没有任何方法可以将其指定给GROUP BY
条款本身。
答案 2 :(得分:0)
你可能想要这样的东西。我假设(ContentPrvdr,LocalDatabaseId,LastUpdated,CMCustomerID)是唯一的。如果不是这种情况,则需要在数据库中指定 唯一的内容,因为它似乎没有任何明显的主键。
SELECT T4.* FROM (
SELECT T2.ContentPrvdr, T2.LocalDatabaseId, MIN(T2.LastUpdated) AS LastUpdated
FROM (
SELECT ContentPrvdr, MIN(LocalDatabaseId) AS LocalDatabaseId
FROM local_database
WHERE CMCustomerID = 10
GROUP BY ContentPrvdr) AS T1
JOIN local_database AS T2
ON T1.ContentPrvdr = T2.ContentPrvdr
AND T1.LocalDatabaseId = T2.LocalDatabaseId
WHERE CMCustomerID = 10
GROUP BY ContentPrvdr, LocalDatabaseId
) AS T3
JOIN local_database AS T4
ON T3.ContentPrvdr = T4.ContentPrvdr
AND T3.LocalDatabaseId = T4.LocalDatabaseId
AND T3.LastUpdated = T4.LastUpdated
WHERE CMCustomerID = 10
ORDER BY LocalDatabaseId, LastUpdated
LIMIT 3
这是我用来测试查询的数据:
CREATE TABLE local_database (
CMCustomerID int NOT NULL,
ContentPrvdr int NOT NULL,
LocalDatabaseId int NOT NULL,
LastUpdated int NOT NULL,
Website int NOT NULL);
INSERT INTO local_database
(CMCustomerID, ContentPrvdr, LocalDatabaseId, LastUpdated, Website)
VALUES
(10, 1, 2, 2, 1),
(11, 1, 2, 2, 1),
(11, 1, 1, 1, 2),
(11, 1, 2, 1, 3),
(10, 2, 2, 2, 4),
(10, 2, 1, 3, 5),
(10, 2, 1, 2, 6),
(11, 3, 3, 3, 7),
(10, 4, 4, 4, 8),
(10, 5, 5, 5, 9);
这是我获得此数据的结果:
10, 2, 1, 2, 6
10, 1, 2, 2, 1
10, 4, 4, 4, 8
如果这不正确,请建议调整测试数据和/或预期结果,以显示您的需求。