SQL Incorrect syntax near the keyword 'order' - mysql

I am writing in SQL and trying to update a new colunm "rankFeild" in table "seker_w_adress". The updating is just fine, but when I am trying to order the rows selected so the colunm will get the biggest value exist for this ID, I am getting this error:
Incorrect syntax near the keyword 'order'.
Here is my code:
update seker_w_adress set rankFeild=si
from seker_w_adress as swa
join
(
select ID,gender,age,seker_ind as si
from last_seker
)ls2
on swa.ID=ls2.ID and ((ls2.gender='female' and ls2.age<47) or (ls2.gender='male' and ls2.age<45))
order by ls2.si desc;
I tried to put "order by" into the ls2 brackets, like that:
update seker_w_adress set rankFeild=si
from seker_w_adress as swa
join
(
select ID,gender,age,seker_ind as si
from last_seker
order by ls2.si desc
)ls2
on swa.ID=ls2.ID and ((ls2.gender='female' and ls2.age<47) or (ls2.gender='male' and ls2.age<45))
;
but it produces another error. What's the problem?

I found the answer - thanks to Akina:
update seker_w_adress set rankFeild=si
from seker_w_adress as swa
join
(
select ID,max(seker_ind) as si
from last_seker
group by ID
)ls2
on swa.ID=ls2.ID
join last_seker as ls
on ls.ID=ls2.ID and ((ls.gender='female' and ls.age<47) or (ls.gender='male' and ls.age<45));

Related

How to resolve ambiguous error in the query

I am using laravel framework for developing API's ,i have one query that is executed without where condition without any error i need to execute with where condition but it's throwing an error
query
select count(*) as aggregate
from `users`
left join `books` on `books`.`book_id` = `books`.`id`
where `access_id` = 5054
SQL Error [1052] [23000]: Column 'access_id' in where clause is
ambiguous
after searching google i got something we have to specify reference of a table name , i added reference name like this where users.access_id =5054 but it's throwing an error like unknown column but in my db i have that column in both users and books table
The problem is its consider as a column so that's why syntax error is coming,try following way it will resolve your problem
select count(*) as aggregate
from `users`
left join `books` on `books`.`book_id` = `books`.`id`
where `users`.`access_id` = 5054

MySql - Update with Select

I need to update a table with a select inside. This is my query so far:
Update T_STATO_CASA
Set UTENTE = 'Admin'
Where ID_CASA in (
Select ID
From T_CASA
Where ID_RICHIESTA
In (437869, 437233, 437235, 437876)
)
But it returns the following error: "Subquery returned more than 1 value. This is not permitted when the subquery follows
=, !=, <, <= , >, >=
or when the subquery is used as an expression."
The subquery returns exactly 4 results if I run it separately. Is my syntax wrong? Thanks.
EDIT: all the suggested solutions that are using JOIN give me syntax error, as if MySql expects only the update-set-where command sequence. For instance I cannot write something like
update T_STATO_CASA as x
set [...]
where [...]
because it gives me syntax error: "Incorrect syntax near word AS. Expected SET"
UPDATE t_stato_casa x
JOIN t_casa y
ON y.id = x.id_casa
SET x.utente = 'admin'
WHERE y.id_richiesta IN(437869, 437233, 437235, 437876)
The reason is that your subquery will return more than one row of record
The more suitable way is to use JOIN instead
UPDATE T_STATO_CASA
JOIN T_CASA t
ON t.id = ID_CASA
AND t.ID_RICHIESTA
IN (437869, 437233, 437235, 437876)
SET UTENTE = 'Admin
If you still want to use subquery,you need to use group_concat to make the result into one record
This was the right syntax for my case:
UPDATE t_stato_casa
SET
UTENTE = 'Admin'
FROM
t_stato_casa AS sc
INNER JOIN T_CASA AS c
ON c.ID = sc.ID_CASA
WHERE
c.ID_RICHIESTA in (437869, 437233, 437235, 437876)
Thanks to everyone.

Prestashop - List view filter

I insert an other column in the list view of my module inserting the values with getList function, I modified the sql to filter in the renderList function but I can't use the alias in where clause.
How can I fix it?
The error i got is the next:
Uncaught Unknown column 'product_supplier_name' in 'where clause'<br /><br />
SELECT SQL_CALC_FOUND_ROWS a.* , s.name AS product_supplier_name FROM ps_supplier_bill a LEFT JOIN ps_supplier s ON s.id_supplier = a.id_product_supplier WHERE 1 AND product_supplier_name LIKE '%fa%' ORDER BY product_supplier_name asc LIMIT 0,50
The proper query should be this:
SELECT SQL_CALC_FOUND_ROWS a.* , s.`name` AS product_supplier_name FROM `ps_supplier_bill` a LEFT JOIN `ps_supplier` s ON s.`id_supplier` = a.`id_product_supplier` WHERE 1 AND s.`name` LIKE '%fa%' ORDER BY s.`name` asc LIMIT 0,50
It's not possible use directly an alias in WHERE, because chronologically, WHERE happens before SELECT, which always is the last step in the execution chain. REFER
From MySQL doc:
Standard SQL disallows references to column aliases in a WHERE clause. This restriction is imposed because when the WHERE clause is evaluated, the column value may not yet have been determined.
MySQL doc

Delete duplicates for multiple columns in JOIN on same table

I am trying to make a delete from joined same table like this:
DELETE FROM `sp10_seo_url` AS sp1 JOIN
(
SELECT seo_url_pk, COUNT(*) AS maxc
FROM `sp10_seo_url`
GROUP BY seo_url_entity_type, seo_url_entity_id, seo_url_language_fk
HAVING maxc > 1
) AS sp2
ON sp1.seo_url_pk = sp2.seo_url_pk
However I am getting a mysql error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS sp1 JOIN ( SELECT seo_url_pk, COUNT(*) AS maxc FROM `sp10_s' at line 1
And I am not sure at all where the error is. The inner query runs just fine and returns the expected set of results. The "ON" keys are properly named (same since we are talking about the same table).
I guess the idea of the query is pretty clear (clean the table of different rows have the same set of values for the three "group by" columns. Is there another way to do this?
Thanks!
you can "cheat" mysql with a double indirection (as explained here Deleting a row based on the max value):
delete from `sp10_seo_url`
where seo_url_pk in (
select seo_url_pk from (
SELECT seo_url_pk
FROM `sp10_seo_url` sp1,
(
SELECT seo_url_entity_type, seo_url_entity_id, seo_url_language_fk
FROM `sp10_seo_url`
GROUP BY seo_url_entity_type, seo_url_entity_id, seo_url_language_fk
HAVING count(*) > 1
) sp2
where sp1.seo_url_entity_type = sp2.seo_url_entity_type
and sp1.seo_url_entity_id = sp2.seo_url_entity_id
and sp1.seo_url_language_fk = sp2.seo_url_language_fk
) t
);
http://sqlfiddle.com/#!2/899ff5/1

getting unknown column error even when the column is present in mysql table

select tab1.*(
select a.*
from fw_invi a left join fw_resp b on a.id=b.did,
fw_resp fra left join(
select *
from fw_type
) tab4 on fra.qaild=tab4.qdetailid //this causing error
)tab1 left join jos_users u on tab1.consu=u.id order by tab1.createdon desc
On running the above query in mysql i am getting the following error,which should not be the case as the specified missing column is present in that table.i think i am doing the wrong nested table aliasing.
Unknown column 'tab4.qdetailid' in 'on clause'
1.Why i am getting the error even though the column is present.?
2.Is my above query syntax correct?
Thanks in advance.
* can't be used with aliases. Aliases are used for named columns.