2016-09-23 129 views
-1

尝试对数据库进行一些查询,但我遇到了问题。有人可以帮我正确查询吗?我的MYSQL查询错误?

这就是我需要

SELECT 
    IdNews, Caption, NewsText, NumberOfViews, NumberOfComments, 
    PublishDate, CoinValue, category.IdNewsCategory, 
    NewsCategory, country.IdCountry, CountryName, news.IdUser, 
    FirstName, LastName, Picture, DateTimeUpCoin 
FROM 
    news 
INNER JOIN 
    category ON category.IdNewsCategory = news.IdNewsCategory 
INNER JOIN 
    country ON news.IdCountry = country.IdCountry 
INNER JOIN 
    user ON user.IdUser = news.IdUser 
WHERE 
    news.IdCountry = 1 
LIMIT 0, 10 
ORDER BY 
    NumberOfViews DESC 
+1

与尝试'WHERE news.IdCountry = 1' –

+0

我刚编辑我的问题,你能帮帮我,谢谢 –

+0

有什么错误? –

回答

2

您已经为Order by写了错误的查询。

在查询中,LIMIT总是最后。

你需要写更正查询按如下:

SELECT 
    IdNews,Caption,NewsText,NumberOfViews,NumberOfComments, 
    PublishDate,CoinValue,category.IdNewsCategory, 
    NewsCategory,country.IdCountry,CountryName,news.IdUser, 
    FirstName, LastName,Picture, DateTimeUpCoin 
FROM news 
INNER JOIN category ON category.IdNewsCategory = news.IdNewsCategory 
INNER JOIN country ON news.IdCountry = country.IdCountry 
INNER JOIN user ON user.IdUser = news.IdUser 
WHERE news.IdCountry = 1 
ORDER BY NumberOfViews DESC 
LIMIT 0, 10 
0

IdCountry列存在于表newscountry。这就是它显示错误的原因。在IdCountry之前加上where子句中的表名。在LIMIT之前放ORDER BY

SELECT IdNews,Caption,NewsText,NumberOfViews,NumberOfComments,PublishDate,CoinValue,category.IdNewsCategory, NewsCategory,country.IdCountry,CountryName,news.IdUser,FirstName, LastName,Picture, DateTimeUpCoin 
    FROM news 
    INNER JOIN category ON category.IdNewsCategory = news.IdNewsCategory 
    INNER JOIN country ON news.IdCountry = country.IdCountry 
    INNER JOIN user ON user.IdUser = news.IdUser 
    WHERE news.IdCountry =1 
    ORDER BY NumberOfViews DESC 
    LIMIT 0 , 10 
+0

我也有订购? –

+0

@MiomirDancevic尝试使用ORDER BY news.NumberOfViews DESC –

+0

在限制之前 –