我的MYSQL查询出错?

时间:2016-09-23 06:36:08

标签: 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

2 个答案:

答案 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

答案 1 :(得分:0)

IdCountrynews中都有

country列。这就是它出现错误的原因。在IdCountry子句中的where之前添加表名。将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