1066 - Not unique table/alias: - mysql

Can you please help me out. I have this SQL query:
SELECT `a`.`UNIQ_CODE`, `b`.`JUDUL_BERITA`, `b`.`THUMNAIL`, `c`.`NAMA`,
`a`.`TAG`, `a`.`CREATED_DATE`, `a`.`GLOBAL`, `a`.`VERIFIKASI`
FROM (`q_master_berita` a)
JOIN `q_master_berita` a ON `b`.`UNIQ_CODE` = `a`.`UNIQ_CODE`
RIGHT JOIN `q_daftar_user` c ON `a`.`ID_UPLOADER` = `c`.`ID`
WHERE ( a.FLAG = 'DRAFT' AND a.ID_SEKOLAH = '3824' )
ORDER BY `b`.`JUDUL_BERITA` asc
LIMIT 10
And somehow it says
Not unique table/alias: 'a'

That's because you are using same alias a twice for table q_master_berita. You need to use different aliases.
SELECT `a`.`UNIQ_CODE`, `b`.`JUDUL_BERITA`, `b`.`THUMNAIL`, `c`.`NAMA`,
`a`.`TAG`, `a`.`CREATED_DATE`, `a`.`GLOBAL`, `a`.`VERIFIKASI`
FROM (`q_master_berita` a)
JOIN `q_master_berita` b ON `b`.`UNIQ_CODE` = `a`.`UNIQ_CODE`
RIGHT JOIN `q_daftar_user` c ON `a`.`ID_UPLOADER` = `c`.`ID`
WHERE ( a.FLAG = 'DRAFT' AND a.ID_SEKOLAH = '3824' )
ORDER BY `b`.`JUDUL_BERITA` asc
LIMIT 10

Related

UPDATE VALUE IN MYSQL MARIA DB WITH JOIN

I am trying to change the value of a column table with a sum function, this is my code:
For example
c.total = (10-2-3) - (3)
c.total = 2
update tabC c
JOIN tabB b ON b.c_id = c.id
set c.total = (c.v1 - c.v2 - c.v3) - IF(sum(b.payment) is not null, sum(b.payment), 0)
where c.id= 983;
but I get the following error:
ERROR 1111 (HY000): Invalid use of group function
I think the error is sum, but how can I solve that?
Thanls in advance
You need to join with a subquery that uses GROUP BY.
UPDATE tabC c
LEFT JOIN (
SELECT c_id, SUM(payment) AS total_payment
FROM tabB
GROUP BY c_id) AS b ON b.c_id = c.id
SET c.total = (c.v1 - c.v2 - c.v3) - IFNULL(b.total_payment, 0)
WHERE c.id = 983
You should usee a nested subquery whit aggregated resutl for join
update tabC c
JOIN (
select c_id, ifnull(sum(payment),0) tot_payment
from tabB
group by c_id
) b ON b.c_id = c.id
set c.total = (c.v1 - c.v2 - c.v3) - b.tot_payment
where c.id= 983;

Error related to only_full_group_by in MySql

I have created Query Which Gives error of only_full_group_by. I Want To change Query Not SET sql_mode=only_full_group_by
#1055 - Expression #4 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'hrdk.s.item_stock_id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by#
This is the query that is giving me trouble:
SELECT `s`.`department_id`, `s`.`category_id`, `s`.`item_id`, `s`.`item_stock_id`, `s`.`tunch`, `cat`.`category_name`, `im`.`item_name`, `im`.`stock_method`, `cat`.`category_group_id`, SUM(s.grwt) AS grwt, SUM(s.ntwt) AS ntwt, sum(s.less) AS less, SUM(s.fine) AS fine
FROM `item_stock` `s`
LEFT JOIN `item_master` `im` ON `im`.`item_id` = `s`.`item_id`
LEFT JOIN `account` `pm` ON `pm`.`account_id` = `s`.`department_id`
LEFT JOIN `category` `cat` ON `cat`.`category_id` = `s`.`category_id`
WHERE (im.stock_method = 1 AND (`s`.`grwt` =0 OR `s`.`grwt` !=0)
OR (`im`.`stock_method` = 2 AND `s`.`grwt` != 0))
AND s.department_id IN(26,27,28,29,30,31,32,59)
AND `s`.`grwt` !=0
AND `s`.`department_id` = '26'
GROUP BY `s`.`category_id`, `s`.`item_id`, if(`im`.`stock_method` = 1, `s`.`tunch`, "")
ORDER BY `s`.`item_stock_id` DESC
Let me know if you need more information.
You've to add all non aggregated columns in group by
SELECT s.department_id, s.category_id, s.item_id, s.item_stock_id, s.tunch, cat.category_name, im.item_name, im.stock_method, cat.category_group_id, SUM(s.grwt) AS grwt, SUM(s.ntwt) AS ntwt, sum(s.less) AS less, SUM(s.fine) AS fine
FROM item_stock s LEFT JOIN item_master im ON im.item_id = s.item_id
LEFT JOIN account pm ON pm.account_id = s.department_id
LEFT JOIN category cat ON cat.category_id = s.category_id
WHERE (im.stock_method = 1 AND (s.grwt =0 OR s.grwt !=0) OR (im.stock_method = 2 AND s.grwt != 0))AND s.department_id IN(26,27,28,29,30,31,32,59) AND s.grwt !=0 AND s.department_id = '26'
GROUP BY s.department_id, s.category_id, s.item_id, s.item_stock_id, s.tunch, cat.category_name, im.item_name, im.stock_method, cat.category_group_id
ORDER BY s.item_stock_id DESC

Update mysql table using CASE subquery with limit

I want to run below query to update Vouchers table
UPDATE vouchers SET status = 6 WHERE voucher_id IN
(
select a.voucher_code from products a
JOIN order b ON a.order_id = b.id
WHERE a.voucher_code != '' LIMIT 10
)
but when running the query, I am getting below mysql error :
This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
Thanks in advance.
Try moving your subquery into an update join:
UPDATE vouchers v
INNER JOIN
(
SELECT a.voucher_code
FROM products a
INNER JOIN order b
ON a.order_id = b.id
WHERE a.voucher_code != ''
LIMIT 10
) t
ON v.voucher_id = t.voucher_code
SET v.status = 6;
LIMIT should be allowed in this case.

MySQL QUERY runs on localhost, but does not on server

I have this QUERY:
select
a.*
from
mt_proyecto a,
mt_mockup b,
mt_diseno c,
mt_modulo d
where
a.estado = 'A' and
(
(b.encargado = '1' and b.idproyecto = a.idmtproyecto) or
(c.encargado = '1' and c.idproyecto = a.idmtproyecto) or
(d.encargado = '1' and d.idproyecto = a.idmtproyecto)
)
group by
a.idmtproyecto
order by a.finalizado asc, a.feccrea desc
Result:
Then, I run the same code on server with the same database:
Is there any problem with the query?
It seems that the query is running correctly on your server, and returning no rows. Please make sure you have the same table contents on your local machine and your server.
Other things:
Pro tip: never use SELECT * or SELECT table.* in software. Always enumerate the columns you want in your result set.
Unless you use GROUP BY with aggregate functions like SUM() or `COUNT(), and naming the correct columns from the result set, it returns unpredictable results. Read this. http://dev.mysql.com/doc/refman/5.1/en/group-by-extensions.html
I solved with this QUERY:
select
a.*
from
(
mt_proyecto a
left join mt_mockup b on
b.idproyecto = a.idmtproyecto
left join mt_diseno c on
c.idproyecto = a.idmtproyecto
left join mt_modulo d on
d.idproyecto = a.idmtproyecto
left join mt_integracion e on
e.idproyecto = a.idmtproyecto
left join mt_pruebas_internas f on
f.idproyecto = a.idmtproyecto
)
where
a.estado = 'A' and
(
(a.idmtproyecto = b.idproyecto and
b.encargado = '1' ) or
(a.idmtproyecto = c.idproyecto and
c.encargado = '1' ) or
(a.idmtproyecto = d.idproyecto and
d.encargado = '1' ) or
(a.idmtproyecto = e.idproyecto and
e.encargado = '1' ) or
(a.idmtproyecto = f.idproyecto =
f.encargado = '1' )
)
group by a.idmtproyecto
order by
a.finalizado asc, a.feccrea desc
Thanks everyone for answer me. I'll do your suggestions .

Strange: MySQL Left Join, "unknown column"; the same leftjoin doesn't work in multiple-table query

I'm trying to run a select query, which is the following:
SELECT * FROM pages, c_item_category cc
LEFT JOIN pages_tr ON (pages.page_id = pages_tr.page_id AND lang_id = 2)
LEFT JOIN users ON (pages.page_author = users.u_id)
WHERE (pages.page_id = cc.item_id AND cc.cat_id = 7)
AND (page_date >= 1317420000 AND page_date <= 1320101999)
AND (page_showinfos = 1)
ORDER BY page_date ASC LIMIT 0,10
But I get this error: Unknown column 'pages.page_id' in 'on clause'
Why is that? I can confirm that I have a column named 'page_id' in 'pages' :)
But, when I do a single-table query with the same left joins, it works well:
SELECT * FROM pages
LEFT JOIN pages_tr ON (pages.page_id = pages_tr.page_id AND lang_id = 2)
LEFT JOIN users ON (pages.page_author = users.u_id)
WHERE (page_date >= 1317420000 AND page_date <= 1320101999)
AND (page_showinfos = 1)
ORDER BY page_date ASC LIMIT 0,10
What is my mistake in the first query? :\
Try it this way instead.
SELECT *
FROM pages
LEFT JOIN pages_tr ON (pages.page_id = pages_tr.page_id AND lang_id = 2)
LEFT JOIN users ON (pages.page_author = users.u_id),
c_item_category cc
WHERE (pages.page_id = cc.item_id AND cc.cat_id = 7)
AND (page_date >= 1317420000 AND page_date <= 1320101999)
AND (page_showinfos = 1)
ORDER BY page_date ASC LIMIT 0,10