2016-09-23 75 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

在限制之前 –