2017-07-28 7 views
-1

'cushbu_mark_user_favorites' cushbu_users (id,first_name,last_name)MySQLがトリプルエラーに参加し、私は私のデシベル</p> <p>1)で3つのテーブルを持っている

2)cushbu_art (id,user_id(FK cushbu_user),title,base_price etc...) -forストアのユーザー芸術

3)cushbu_mark_user_favorites (id,user_id(FK cushbu_user),art_id(FK cushbu_art)) -forマーキングお気に入り項目

私は を取得したい特定のユーザーのすべての芸術項目が
をお気に入り化(favourited)お気に入りのカウントはここ( cushbu_mark_usier_favoritesテーブルに格納されている)各芸術

は私のクエリは

SELECT 
    cushbu_art.id AS art_id, 
    cushbu_art.title, 
    cushbu_art.base_price, 
    cushbu_art.image_name, 
    CONCAT(
     cushbu_users.first_name, 
     ' ', 
     cushbu_users.last_name 
    ) AS artist_name,COUNT(cushbu_mark_user_favorites.art_id) 
FROM 
    cushbu_art 
JOIN cushbu_mark_user_favorites ON cushbu_mark_user_favorites.art_id = cushbu_art.id 
JOIN cushbu_users ON cushbu_users.id = cushbu_art.artist_id 
LEFT JOIN cushbu_mark_user_favorites ON cushbu_art.id=cushbu_mark_user_favorites.art_id 
WHERE 
    cushbu_mark_user_favorites.user_id = 68 

ですが、私はNot unique table/alias: 'cushbu_mark_user_favorites'このjoin声明

LEFT JOIN cushbu_mark_user_favorites ON cushbu_art.id=cushbu_mark_user_favorites.art_id 

UPDATE

SELECT 
    a.id AS art_id, 
    a.title, 
    a.base_price, 
    a.image_name, 
    CONCAT(
     c.first_name, 
     ' ', 
     c.last_name 
    ) AS artist_name,COUNT(b.art_id) 
FROM 
    cushbu_art a 
JOIN cushbu_mark_user_favorites b ON b.art_id = a.id 
JOIN cushbu_users c ON c.id = a.artist_id 
LEFT JOIN b ON a.id=b.art_id 
WHERE 
    b.user_id = 68 
を得ました
+0

だから不明なことは...?あなたは同じテーブルに複数回参加しているので、エイリアスを使用する必要があります。そのため、いつどのインスタンスを参照しているかわかりません。 – CBroe

+0

@chiragsataparaは説明バージョンです。https:// stackoverflow .com/questions/45373911/mysql-joinget-total-favorites-for-each-row – Jabaa

+0

確かに私はそれを受け入れるでしょう – Jabaa

答えて

0

以下の質問をお試しください。

SELECT 
    cushbu_art.id AS art_id, 
    cushbu_art.title, 
    cushbu_art.image_name, 
    CONCAT(
     cushbu_users.first_name, 
     ' ', 
     cushbu_users.last_name 
    ) AS artist_name , b.favorites_count as total_fav 
FROM 
    cushbu_mark_user_favorites 
LEFT JOIN cushbu_art ON cushbu_art.id=cushbu_mark_user_favorites.art_id 
LEFT JOIN cushbu_users ON cushbu_users.id = cushbu_art.artist_id 
LEFT JOIN (SELECT art_id,count(*) as favorites_count FROM cushbu_mark_user_favorites GROUP BY art_id) as b ON b.art_id=cushbu_art.id 
WHERE cushbu_mark_user_favorites.user_id=1 
GROUP BY cushbu_art.id 

希望はあなたに役立ちます。

+0

エラー 'n GROUP BYのない集計クエリ、SELECTリストの式#1集約されていない列 'cushbu_db.cushbu_art.id'が含まれています。これはsql_mode = only_full_group_by'と互換性がありません – Jabaa

+0

別のエラー '' where句 'の' cushbu_mark_user_favorites.user_id 'が不明です。 – Jabaa

+0

エラーはありませんが、iCoutの結果は間違っています。常に1です。 – Jabaa

関連する問題