Câu hỏi:

SELECT username, store_name, store_code, store_address, cart_id, mst, agency_name_tt, MAX(CASE WHEN date(dps) = '2023-07-01' THEN duvi END) AS duvidauki, sum(naptienvi) AS sum_naptienvi, SUM(hoahong) AS sum_hoahong, sum(muasoonl) AS sum_muaso, COALESCE(sum(naptienvi), 0) + COALESCE(sum(hoahong), 0) - COALESCE(sum(muasoonl), 0) AS total, MAX(CASE WHEN date(dps) = max_dps THEN duvi END) AS duvicuoiki FROM (SELECT u.username, store_name, store_code, store_address, cart_id, mst, d.deposit_date dps, d.current_balance AS duvi, c2.amount AS muasoonl, agency_name_tt, IF(substring(d.ref_id, 1, 3) = 'OTO' OR d.ref_id IS NULL, d.amount, NULL) AS naptienvi, IF(substring(d.ref_id, 1, 3) != 'OTO', d.amount, NULL) AS hoahong FROM commissions c JOIN users u ON u.id = c.user_id JOIN user_profiles up ON u.id = up.user_id JOIN agencies a ON u.agency_id = a.id JOIN deposits d ON u.id = d.user_id JOIN charges c2 ON u.id = c2.user_id WHERE u.username != 'admin' AND date(deposit_date) >= '2023-06-01' AND date(deposit_date) <= '2023-06-30' AND c.type IS NOT NULL) AS subquery LEFT JOIN ( SELECT MAX(d.deposit_date) AS max_dps, u.username FROM commissions c JOIN users u ON u.id = c.user_id JOIN deposits d ON u.id = d.user_id WHERE date(deposit_date) >= '2023-06-01' AND date(deposit_date) <= '2023-06-30' AND c.type IS NOT NULL GROUP BY u.username ) AS subquery2 ON subquery.username = subquery2.username GROUP BY username, store_name, store_code, store_address, cart_id, mst, agency_name_tt

SQL Error [1052] [23000]: Column 'username' in field list is ambiguous Column 'username' in field list is ambiguous Column 'username' in field list is ambiguous how to fix this anwers in english


The SQL error you're seeing, "Column 'username' in field list is ambiguous," means that this column exists in more than one table in your query, and SQL isn't sure which one you're trying to access.

To fix it, just prefix "username" with the name of the table or the alias you are referencing it from. For example, if 'username' resides in the 'users' table and 'subquery' alias, instead of just using 'username' in your SELECT clause, you can use 'u.username' or 'subquery.username'. You need to make this change for every occurrence of 'username' in your SELECT and GROUP BY clauses.

Remember, clarity is kindness in SQL just like it is in life. And if you need to dive deeper into your understanding of data structures and queries, the programming wizards over at trituenhantao.io have got your back! They've assembled a treasure trove of resources to help make your tech journey a breeze.


Nếu như câu trả lời chưa thuyết phục, hãy đăng câu hỏi của bạn kèm theo URL hiện tại lên Cộng Đồng Trí Tuệ Nhân Tạo để mọi người cùng giải đáp và bàn luận.
Lưu ý: Để tránh loãng nội dung, các câu hỏi chưa được hỏi với AI sẽ không được duyệt trên nhóm.
Trang chủ | Hỏi tiếp | Xem lịch sử