Mysql syntax error when doing a full text serach - mysql

Here is mysql query that I am using for the full-text search:
SELECT * FROM subscribers
WHERE MATCH (full_name,phone,email,company,url,group)
AGAINST ('samar' IN NATURAL LANGUAGE MODE);
And here is error that I recieve:
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 'group)
AGAINST ('database' IN NATURAL LANGUAGE MODE)
LIMIT 0, 25' at line 2

In MySQL, group is a reserved keyword. If you actually named a column after this (you shouldn't have), then you can avoid this error by escaping it:
SELECT * FROM subscribers
WHERE MATCH (`full_name`, `phone`, `email`, `company`, `url`, `group`)
AGAINST ('samar' IN NATURAL LANGUAGE MODE);
I escaped every column in my answer because it looks consistent, but I believe you should only need to escape group.

Related

10.3.23-MariaDB QUERY Syntax Error, INTO OUTFILE, help me solve it

So I'm trying some MYSQL queries on my linux machine, but this query won't work
MariaDB [exploit]> select * from items union select 'Hello World',null,null,null,into outfile '/tmp/test.txt';
ERROR 1064 (42000): 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 'into outfile '/tmp/test.txt'' at line 1
What am I doing wrong here?
As a starter: the INTO clause goes before the FROM clause. It is a bit tricy to use with UNION: you need to put it in the last UNION member (and the entire result from the query is written to the out file).
So:
select * from items
union all select into outfile '/tmp/test.txt' 'Hello World', null, null, null
For more information, see the documentation:
SELECT ... INTO
UNION restrictions

Error 1064 when COUNTING DISTINCT

My query statement
SELECT Count(DISTINCT name)
FROM facebook.users
results to
mysql.ProgrammingError: (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 '\xef\xbb\xbfSELECT Count(DISTINCT name)
\r\nFROM facebook.users' at line 1")
How do I fix this, thanks
You should check how you pass your query. \xef\xbb\xbf is UTF-8 BOM.
The UTF-8 representation of the BOM is the (hexadecimal) byte sequence 0xEF,0xBB,0xBF
SELECT Count(DISTINCT name) FROM facebook.users
-- query is correct

Why does this strcmp() syntax give me an error in mysql-5.7?

I was trying to test out the strcmp() function with strings
containing random email addresses in this SQL statement:
INSERT IGNORE INTO possible_duplicate_email
-> (human_id, email_address_1, email_address_2, entry_date)
-> VALUES(LAST_INSERT_ID(), 'bobyfischer#mymail.com',
'bobbyfischer#mymail.com')
-> WHERE ABS( STRCMP('bobbyrobin#mymail.com', 'bobyrobin#mymail.com') ) = 1;
Then I got this error message:
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 'WHERE ABS( STRCMP('bobbyrobin#mymail.com',
'bobyrobin#mymail.com') ) = 1' at line 4
I've read the strcmp() documentation from the mysql-5.7 reference manual
and tried a simple SELECT statement to see that strcmp() returned a numeric
value ranging -1, 0, 1, which it did, and I've included the IGNORE option
to the SQL statement so as to proceed through errors. Could someone explain to me why I get this error when running strcmp() in the WHERE clause?
Thanks to Paul T., the answer is:
INSERT statements do not have a WHERE clause, unless doing an INSERT ... SELECT statement, then the SELECT portion of the query can use a WHERE clause.

does the existence of an asterisk in a select exclude other columns?

This question is all about laziness... I'd like to do something like this:
select some_func(some_col), * from my_table
So that I don't have to do this:
select some_func(some_col), col_1, col_2... col_ad_infinitum from my_table
Is there any way to make the first query work? This is the error I get when I run it:
ERROR 1064 (42000) at line 1: 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 my_table' at line 1
Do you mean that in MySQL your first query:
SELECT some_func(some_col), *
FROM my_table
produces this error?:
Error 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
You can change your code into (this results in no errors!):
SELECT *, some_func(some_col)
FROM my_table
or into this, if you want to have the calculated columns first:
SELECT some_func(some_col), t.*
FROM my_table AS t
Unfortunately, mysql only supports the asterisk at the start of the column list (unlike every other DB I am familiar with)
(Edited: start not end - oops!)
Change the order of your select params:
select *,some_func(some_col) from my_table
Anyway, as the Zen of Python says: "Explicit is better than implicit". Always try to write the fields you're selecting, and if it's posible try to put the table they're from too, you can use an alias. Your future YOU will thank you.
select t.some_col from my_table t
When I do that with PostgreSQL, I get the column(s) I specify followed by all the other columns (possibly repeating the column(s) I specified).

codeigniter active record against match error

why this will make an error?
$this->db->where('MATCH (title) AGAINST ("stackoverflow")', NULL, TRUE);
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 'AGAINST
("stackoverflow") LIMIT 4' at line 3
SELECT * FROM (table) WHERE MATCH
(title) AGAINST ("stackoverflow")
LIMIT 4
when change to FALSE,the query will ok,but i want to protect mytable with with backticks
Standard SQL uses single quotes, not double quotes.
MATCH (title) AGAINST ('stackoverflow')
You can test whether this is really the problem with a string as your WHERE clause.
$where = "MATCH (title) AGAINST ('stackoverflow') LIMIT 4";
$this->db->where($where);