I want select 2 Rows from the mysql table with additional "LIKE" and "AND" clauses..
Wit like clause I want to find only word starting with "a%"..
But I can't find the syntax error. Can you give me some hints.??
SELECT word,description
FROM word
WHERE(`language` = CONVERT( _utf8 'Tedi' USING armscii8 ) AND like 'a%') AND `visible` =1
many thanks in advance.
Regards,
Koko
The syntax error is a missing expression before the LIKE comparison operator.
We'll have to guess what expression you wanted to do the comparison operation on, so I'll just choose the first column from the SELECT list, to demonstrate:
SELECT w.word
, w.description
FROM word w
WHERE w.language = CONVERT( _utf8 'Tedi' USING armscii8 )
AND w.word LIKE 'a%'
AND w.visible = 1
The predicates in the WHERE clause specify the criteria that a row has to satisfy before it will be returned, it doesn't care whether that's zero rows, two rows or a brazilian rows.
Related
I am not getting the {m, n} syntax of MySql. What is it? How this is used in queries?
I am writing queries on Like statements and regex statements.
This is my query:
select
distinct city
from
station
where
city regexp 'a{m, n}';
Just ignore the query. Just tell me how this is used. Added query to fool the Stack Overflow. It is always saying to add code.
This isn’t (really) about MySql. It is about regular expressions. a{m,n} means between m and n times the letter a Consequtively.
So, for example, with where mycolumn regexp 'doh{3,5}' you will match all rows where mycolumn contains dohhh, dohhhh or dohhhhh.
See also https://www.oreilly.com/library/view/oracle-regular-expressions/0596006012/re13.html
you almost got the correct query
{m,n} -> The preceding element or subexpression must occur between m and
n times, inclusive.
So below query, you are trying to get rows with column with 'a' between min and max
select
distinct city
from
station
where
city regexp 'a{1,3}';
Try sqlfiddle
I have a table that contains two TEXT fields with textual content, and in a separate table, a field that contains comma separate values of keywords that can be more than one word. The following query works in my WAMP using Appserv, does not works in our Hostgator LAMP...why??
SELECT
t.content_me, t.content_visitor, t.Id, exp.owner_user_id, exp.name
FROM (SELECT t.content_me, t.content_visitor
FROM `texts` AS t
WHERE t.owner_user_id=1 *<== obviously this changes...*
ORDER BY t.Id DESC) AS t
INNER JOIN exp ON t.Id = exp.owner_user_id AND
t.content_me REGEXP (REPLACE(exp.keywords,',','|'))
WHERE t.owner_user_id=e.owner_user_id=6
ORDER BY t.Id DESC
Furthermore, if I literally put a value in this part:
t.content_me REGEXP (REPLACE(exp.keywords,',','|'))
as lets say:
t.content_me REGEXP ('yeah|ok')
It works in Hostgator. So I guess the problem is that REGEXP (REPLACE(exp.keywords,',','|')) thingy...right?
EDIT
Ok, I simplified the query just for the fun of it :)
SELECT
t.Id, t.text, t.owner_user_id FROM `t`
LEFT JOIN e ON e.owner_user_id = t.owner_user_id
WHERE
t.text REGEXP REPLACE(e.keywords,',','|') AND t.owner_user_id=1
Same results: works in WAMP, doesn't in LAMP. Also if I do literal REGEXP like
REGEXP REPLACE('yeah,can',',','|')
Works in both servers. My guess is that something is happening with
REGEXP REPLACE(e.keywords,',','|')
i.e, having the REGEXP use field content and not literal sting.
EDIT:
Well...now I see the LAMP MySQL throws an error (not happening in WAMP):
Illegal mix of collations (utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,IMPLICIT) for operation 'regexp'
So....
REGEXP REPLACE(e.keywords,',','|') COLLATE utf8_unicode_ci
Fixed it
I'm trying to build a SQL statement to retrieve user names in the following order
at first, return the names that start with Arabic letter, then the names that start with English letters, then the names that start with special characters.
then sort each of the three groups in ascending order.
This is my code:
SELECT `name` FROM `user`
order by case when substring(name,1,1) like 'N[أ-ي]' then 1
when substring(name,1,1) like '[a-zA-Z]' then 2
else 3
end
,name
The problem is that the case part always returns 3, and so the statement sorts the names in the default order(special chars first, then English letters then Arabic letters). What is the problem in my query?
You need to use regex, not like... (because you use regular expression)
SELECT `name` FROM `user`
order by case when substring(name,1,1) regexp 'N[أ-ي]' then 1
when substring(name,1,1) regexp '[a-zA-Z]' then 2
else 3
end
,name
Reference: MySQL CASE statement and REGEXP
I have a code which I recently discovered :) and it does do its job and well done I might add. But, I want to check all columns instead of checking it by column. Is it possible
Check my code below:
SELECT column_name
FROM table_name
WHERE column_name REGEXP '[[.DLE.]-[.US.]]'
Now, what I want is something like this but it won't work
SELECT *
FROM table_name
WHERE REGEXP '[[.DLE.]-[.US.]]'
Kindly advice and I apologize for asking many questions :)
REGEXP is a binary operator which means you have to have a left operand and a right operand.
Like most arithmetic operators.
You could check all columns like this:
SELECT *
FROM table_name
WHERE CONCAT(a, b, c, d, ...) REGEXP '[[.DLE.]-[.US.]]'
I'm using ... for the sake of the example, but you'd need to name all your columns explicitly. There's no option to use a wildcard for the columns inside an expression.
You can't set "all columns" in a single SET clause. You'd need to do something like the following:
UPDATE table_name SET
a = REPLACE(a,char(16),''),
b = REPLACE(b,char(16),''),
c = REPLACE(c,char(16),''),
d = REPLACE(d,char(16),''),
...similar for other columns;
If you think this is an unexpected omission in the SQL language, then I wonder if you can name any other programming language that lets you compare to or assign a value to "all variables" in a single expression?
I have a MySQL query
select query from HR_Health_Logs where query REGEXP 'CPU|MAC|PC|abacus|calculator|laptop|mainframe|microcomputer|minicomputer|machine';
Except that the regex is much longer, and contains many synonyms and misspellings.
I need to cut this short and have a table with all the synonyms and misspellings, so that I can avoid this very long query. So I'm looking for something like
select query from HR_Health_Logs where query REGEXP '**HAVE A TABLE WITH ALL MY SYNONYMS AND MISSPELLINGS SEARCHED HERE**';
How about the ANY function ?
select query from HR_Health_Logs where query REGEXP ANY (SELECT spell FROM misspelled WHERE correct = 'masturbate' ) ;
SELECT query
FROM HR_Health_Logs l, synonym s
WHERE l.query = s.synonym
SELECT query
FROM HR_Health_Logs
WHERE query IN (
SELECT synonym AS query
FROM synonyms_table
WHERE word = 'masturbation'
UNION
SELECT misspelling AS query
FROM misspellings_table
WHERE word = 'masturbation'
)
Assuming your synonyms and misspellings are in two separate tables. Otherwise you'll only use one of the subqueries and drop the UNION.