Mysql Partition error - mysql

I am trying to create a mysql script that will search for results while at the same time filtering out duplicates of a particular field, I did some research and the code I found came in the format of something like this:
SELECT * FROM sites s INNER JOIN (SELECT h.*, row_number() over (PARTITION by muleid) as seqnum from history h ) h WHERE acceptid=2;
But it came up with the 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 '(PARTITION by muleid) as seqnum from history h ) h WHERE acceptid=2' at line 1
Is there anyone that can help?

Related

Update with nested select in from statement does not work mysql

Here is an error I don't understand:
mysql> UPDATE gp
-> SET gp.gpid = gp.new_gpid
-> FROM (
-> SELECT gpid, ROW_NUMBER() OVER (ORDER BY [gpid]) AS new_gpid
-> FROM gp
-> ) gp;
ERROR 1064 (42000): 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 'FROM (
SELECT gpid, ROW_NUMBER() OVER (ORDER BY [gpid]) AS new_gpid
' at line 3
As far as I can tell nested SELECT in a FROM statement seems to be depreciated.
Am using mysql 8.0.21
Any help to make this query work would be greatly appreciated.
Thank you
EDIT 1:
What I am trying to achieve is to update the gpid column with row numbers instead of the actual AUTO_INCREMENT id, which contains gaps in between ids, the explanation in this post Change mysql auto increment id column value
The UPDATE... FROM ... syntax is not supported by MySql.
You can do it with a JOIN:
UPDATE gp
INNER JOIN (
SELECT gpid, ROW_NUMBER() OVER (ORDER BY gpid) AS new_gpid
FROM gp
) t ON t.gpid = gp.gpid
SET gp.gpid = t.new_gpid;
See a simplified demo.
Your nested query is selecting from 'gp' but is then aliased as 'gp' it can't select from itself.

1064 You have an error in your SQL syntax; - shows in my website

I was working on my new joomla website changed some appearing sittings then the syntax error came out in the main page and changed all the website design I tried to undo all the changes I made and the error still there
the message I got
1064 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 ') AND c.access_view IN (0,1,1,5) AND c.restriction_18=0 ORDER BY
i.date_start D' at line 2
SELECT i.*,c.id as c_id, c.name as c_name,c.alias as c_alias,c.icon_url as c_icon_url,
r.name as r_name, img.path as img_path, img.name as img_name, img.ext as img_ext,
img.caption as img_caption
FROM l0ucm_djcf_categories c, l0ucm_djcf_items i
LEFT JOIN l0ucm_djcf_regions r ON r.id=i.region_id
LEFT JOIN ( SELECT img.id, img.item_id, img.name, img.path, img.ext, img.ordering, img.caption
FROM (SELECT * FROM l0ucm_djcf_images WHERE type='item' ORDER BY ordering) img GROUP BY img.item_id ) AS img
ON img.item_id=i.id
WHERE i.date_exp > '2015-07-25 16:44:45' AND i.published = 1 AND c.published = 1
AND i.cat_id=c.id AND i.cat_id IN (9,10,11,12,13,15)
AND i.type_id IN () AND c.access_view IN (0,1,1,5) AND c.restriction_18=0
ORDER BY i.date_start DESC limit 9
anyone can help me to find out from where I can solve this problem?
thank you all,
The problem is in the statement AND i.type_id IN ().
IN() function cannot get an empty value. You should check the php code that creates the query and add a validation to the variable to add the statement only if it not empty.

MySQL select with alias

i have error on sql statment
SELECT temp.* FROM
(SELECT th1.process_id,th2.process_id FROM `thread` as th1,`thread` as th2
where (th1.thread_id=th2.thread_id)and
(th1.process_id!=th2.process_id) and
(th1.analysis_id='".$analysis_id."' ) and
(th2.analysis_id='".$analysis_id."' )) as temp
where ((t emp.p1 NOT IN (select pr.parent_process_id from process as pr
wherer pr.process_id=th2.process_id and (th2.analysis_id='".$analysis_id."' )
and (pr.analysis_id='".$analysis_id."' )))
or (temp.p2 NOT IN
(select pr1.parent_process_id from process as pr1
wherer pr1.process_id=th1.process_id and (th2.analysis_id='".$analysis_id."' )
and (pr1.analysis_id='".$analysis_id."' ))))
You have an obvious syntax error using wherer instead of where.
wherer pr.process_id=th2.process_id and
Should be
where pr.process_id=th2.process_id and
When MySQL reports an error similar to Check the manual for the correct syntax to use near..., look exactly to that location or to the character immediately before it for your syntax error.
check the manual that corresponds to your MySQL server version for the right syntax to use near 'wherer pr.process_id=tem.p2

Sql query doesn't work in Mysql version 5.5.16

I am attempting to execute the following query:
$query = "SELECT O. * AS NUM FROM (ab_order O)
LEFT JOIN ab_user U ON (O.id_user=U.id)
WHERE O.id IN ( SELECT OT.id_ab_order FROM ab_order_transaction OT
LEFT JOIN ab_transaction T ON (OT.id_ab_transaction = T.id)
LEFT JOIN ab_user U ON (T.id_user = U.id)
WHERE T.validated = 1 {$condTrans} ) {$condOrder}
ORDER BY {$orderCol} LIMIT $from, $numRecords ";
$queryDB = $DB->queryExec($query);
On the live server:
it works with MySql version 3.3.10.
PHP version 5.2.17.
But I need to use localhost:
XAMPP for linux, v. 1.7.7
PHP 5.3.8
MySql version 5.5.16.
On localhost it says:
MySQL error: 1064 : 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 NUM
FROM (ab_order O)
LEFT JOIN ab_u' at line 1.
Is any easier way then uprgrade the live server Mysql database?
The aliasing of all the columns seems incorrect: SELECT O. * AS NUM. I'm unsure of why it would work on previous versions, but the as num should either be removed, or each column should be explicitly aliased.
You can define Alias for individual column in SELECT. Here is the issue
SELECT O. * AS NUM
Instead of this use
SELECT O. *
This is useful article Using Column Alias in SELECT Statement

Can I count number of rows in joined table?

Like this:
SELECT s.*, count( logs.* ) as ssh_count
FROM servers s
LEFT JOIN logs ON s.ip_address = logs.server_ip
But I get an error with that query:
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 ssh_count FROM servers s LEFT JOIN logs ON s.ip_address = logs.server_ip LIMIT' at line 1
I think that's because you can't address a table in the count function.
I can do this using a subquery, but that will likely slowly down the query.
What is a better way of doing this?
You can adress a table column, but you can't address table.*, you can do this for example:
SELECT s.*, count( logs.server_ip ) as ssh_count
FROM servers s
LEFT JOIN logs ON s.ip_address = logs.server_ip