How do I run Mysql queries from the bash shell? - mysql

Sorry if the question is badly worded, essentially i'm trying to do something like this
mysql --user user --p password database -e " SELECT * FROM
`wp_postmeta` WHERE `meta_key` LIKE '_sku' AND `meta_value` = '';" | awk ' { print $2 } ' > post_id_data.txt
However when I run this style of code i'm getting errors and i'm not sure why, reason being as the mysql statement is straight from phpmyadmin and works when i connect to the mariadb database.
I have tried to remove some of the back ticks so that they are more like this
'' instead of whats being shown but still no luck instead I get this error
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check
the manual that corresponds to your MariaDB server version for the right
syntax to use near ''wp_postmeta' WHERE 'meta_key' LIKE '_sku' AND
'meta_value' = ''' at line 1
Another example of code that I have tried can be seen below.
#!/bin/bash
MYSQLUSER="user"
MYSQLPASS="password"
database="wordpress"
#mysql options to be parsed
MYSQLOPTS="--user=${MYSQLUSER} --password=${MYSQLPASS} ${database}"
mysql ${MYSQLOPTS} << EOFMYSQL
SELECT * FROM `wp_postmeta` WHERE `meta_key` LIKE '_sku' AND `meta_value` = '';
EOFMYSQL
I Have also tried
root#3e0a62b6b42c:/# mysql --user=user--password=passowrd wordpress -e
"SELECT * FROM 'wp_postmeta' WHERE 'meta_key' LIKE '_sku' AND
'meta_value' = '';"
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''wp_postmeta' WHERE 'meta_key' LIKE '_sku' AND 'meta_value' = ''' at line 1
Please note that i'm new to Mysql so any references to good learning material where I can do thing such as this is deeply appreciated

Remove the semicolon and quotes around column/table names.
The mysql tool explodes when you terminate your query with a semicolon.
Use backticks to quote entity names if you need to, not quotes.
Try this:
"SELECT * FROM wp_postmeta WHERE meta_key LIKE '_sku' AND meta_value = ''"

Related

MySQL replace QUOTE

I need to replace &quote; in a string. I tried to do this:
SET `title` = REPLACE( `title`, '"', '' )
but it gives me a parsing error.
This is the 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 ''&quot)' at line 1
Server version: 5.5.57-cll - MySQL Community Server (GPL)
How do I do this?
Your query should work, but it seems like the error is from another query. Anyhow:
Try this:
UPDATE tbl_name
SET
field_name = REPLACE(field_name,
string_to_find,
string_to_replace)
WHERE
conditions;
Example:
UPDATE bbb_sefurls
SET
metatitle = REPLACE(metatitle,
'&quote;',
'');
No need for a condition
This is simple approach but it replaces all the " in string.
UPDATE dummy_tab SET metatitle =REPLACE(metatitle,'/"','') WHERE metatitle LIKE '%"'
If I'm to read your error 100% as written... you have the word quote spelled wrong some where.
...for the right syntax to use near ''&quot)'
notice in your error it shows no "e" There for your replace statement would also NOT catch this.
Or more closely looking at the image you posted... you are replacing
&quote(semicolon)
with
''
But the error says it is finding the string
&quote)
somewhere in your query... Which would seem to be invalid.
Search your code for
&quot)

Laravel Field Syntax Returns error or access Violation

There seems to be an identical question here, however, the context of that question is based around MariaDB and I am using MySQL. I'm not sure if this is related code or a server configuration?
When I try to query JSON data in Laravel 5.4...
$dataCheck = DB::table('station_data')
->select('entry_data')
->where('entry_data->status', '=', $data)
->where('station_id', '=', $stationID)
->first();
I get the following error...
SQLSTATE[42000]: Syntax error or access violation: 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 '>'$."status"' =
? and station_id = ? limit 1' at line 1 (SQL: select entry_data
from station_data where entry_data->'$."status"' = test and
station_id = 7 limit 1)
I'm not sure why it's adding the $ symbol and messing with the syntax

SQL query doesn't work. Possible syntax error

Seems to be strange but very simple SQL query doesn't work for me.
UPDATE wp_postmeta SET `meta_value` = REPLACE(post_meta, `http://url/`, `http://new_url`)
Error message appears 'a new statement was found but no delimiter was found between it and the previous one (REPLACE) and when I'm trying to execute the code - #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 ')' at line 1. Please advise.
Your specific issue is the use of backticks rather than single quotes. However, I think a better way to express the logic is:
UPDATE wp_postmeta
SET meta_value = CONCAT('http://new_url', SUBSTRING(post_meta, 12) )
WHERE meta_value LIKE 'http://url/%' AND
meta_key = ??;
This uses the WHERE clause to filter out rows that should not be updated. It also ensures that the update is only to the occurrence of 'http://url/' at the beginning the string -- usually the logic that is intended.

MYSQL where clause causes syntax error

I just cannot find what is wrong with this simple statement:
$stat_qry = mysql_query("SELECT * FROM stats WHERE group=$galgroup") or die("STATS ERROR: ".mysql_error()); $stat = mysql_fetch_array($stat_qry);
i just get: "STATS 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 'group=1' at line 1"
i cannot get it to work with the 'where' clause, if i remove 'where' it works but just lists everything in the database
GROUP is a reserved word so it needs to be between back tick ``
Also, if $galgroup can be a string and not only a number, you need to add quotes arround it :
$stat_qry = mysql_query("SELECT * FROM stats WHERE `group`='$galgroup'") or
die("STATS ERROR: ".mysql_error()); $stat = mysql_fetch_array($stat_qry);

Error while changing paths in MySQL

Hi iam trying to change the path in some links in my WordPress database.
In my table wp_commeentmeta, I am using the syntax:
UPDATE table SET meta_value = REPLACE(meta_value, 'http://articles.mydomain.com', 'http://localhost/articles')
BUT I get the following 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 'table SET meta_value = REPLACE(meta_value,
'http://articles.mydomain.com', 'http' at line 1
Any help on this please?
Please note : I am using phpMyAdmin for this.
Change the table to be the name of your table
UPDATE wp_commeentmeta
SET meta_value = REPLACE(
meta_value,
'http://articles.mydomain.com',
'http://localhost/articles'
);
(Just fyi, the linebreaks aren't important, just makes the query more readable)