I am facing a problem while using Select statement where I have a column name as 'Value', and I want to retrieve the values based on the 'value' column with SELECT and LIKE operators.
Code:
SELECT compo.clecompo
FROM compo compo
ON (compo.clecompo = metadatas_compo.clecompo)
AND ((metadatas_compo.value LIKE '%%NOM%%')
OR (metadatas_values.metavalues_name LIKE '%%NOM%%'))
I highlighted the value keyword.. It's a sample of my query where i am not getting any results.
Note: (metadatas_compo.value) metadatas_compo is table name.
You escape literals in MySQL using backticks `
SELECT compo.clecompo FROM compo compo ON (compo.clecompo = metadatas_compo.clecompo) AND ((metadatas_compo.`value` LIKE '%%NOM%%') OR (metadatas_values.metavalues_name LIKE '%%NOM%%'))
However, it is advised to not use reserved literals in your table/column names. For a list of reserved words:
http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html
Related
I am new to SQL and still learning but one thing I am confused about is where we use ` and ' operators in MySQL/MariaDB. Can anyone explain this?
Backticks (`) are used to indicate database, table, and column names. Unless you're using reserved or conflicting words for table and database names, you'll not need to use them.
Quotes (' or ") are used to delimit strings, and differentiate them from column names.
For example:
SELECT * FROM `database`.`table` WHERE `column` = "value";
As I mentioned, backticks aren't needed, if you use reasonable table and column names:
SELECT * FROM mydb.users WHERE username = "jim";
But strings will always need quotes. This query is comparing the value in the column username against a value in the column bob, rather than the string value "bob":
SELECT * FROM mydb.users WHERE username = bob;
I want to to create a regex to find all columns that only have a single character ([A-Z]) as name, like N or M but not NM.
I've tried:
SELECT * FROM 'table' WHERE Name REGEXP '^[A-Z]'
But it's not displaying the expected result.
Try ^[A-Z]$.
You then state that this character is first and also last character of the value.
The regex functions in Oracle work only on one column. So, to search for just one character in a column, you would do the following:
select * from yourTable where REGEXP_LIKE (col1, '^[A-z]$');
Now, to search all the char/varchar columns on your table, you'll need to chain the regex expressions together, like so:
select * from yourTable where REGEXP_LIKE (col1, '^[A-z]$') or REGEXP_LIKE (col3, '^[A-z]$');
SQL solution:
where name in ('N','M')
I'm trying to select all rows that contain only alphanumeric characters in MySQL using:
SELECT * FROM table WHERE column REGEXP '[A-Za-z0-9]';
However, it's returning all rows, regardless of the fact that they contain non-alphanumeric characters.
Try this code:
SELECT * FROM table WHERE column REGEXP '^[A-Za-z0-9]+$'
This makes sure that all characters match.
Your statement matches any string that contains a letter or digit anywhere, even if it contains other non-alphanumeric characters. Try this:
SELECT * FROM table WHERE column REGEXP '^[A-Za-z0-9]+$';
^ and $ require the entire string to match rather than just any portion of it, and + looks for 1 or more alphanumberic characters.
You could also use a named character class if you prefer:
SELECT * FROM table WHERE column REGEXP '^[[:alnum:]]+$';
Try this:
REGEXP '^[a-z0-9]+$'
As regexp is not case sensitive except for binary fields.
There is also this:
select m from table where not regexp_like(m, '^[0-9]\d+$')
which selects the rows that contains characters from the column you want (which is m in the example but you can change).
Most of the combinations don't work properly in Oracle platforms but this does. Sharing for future reference.
Try this
select count(*) from table where cast(col as double) is null;
Change the REGEXP to Like
SELECT * FROM table_name WHERE column_name like '%[^a-zA-Z0-9]%'
this one works fine
I am writing a PHP script where I will search for a keyword in a specific table and return the id of that keyword.
What I have done so far is:
SELECT * FROM 'table' WHERE column_name LIKE '%keyword%'
This returns the entire row of the keyword. What I want is a query that will just return the id of that row, so I can put it in a variable.
First, you should probably replace the quotes around 'table' with backticks, or you'll likely get syntax errors somewhere. The rest of my query examples use backticks for the table name. It's advised to use them for column names too to reduce the chance of conflicts.
Instead of using * to fetch all columns, explicitly list which columns you want to fetch:
SELECT id FROM `table` WHERE column_name LIKE '%keyword%'
The * wildcard selects all columns as you have noticed from your original query.
If you want to select other columns, list them as comma separated names:
SELECT id, foo, bar FROM `table` WHERE column_name LIKE '%keyword%'
Since you are using mySQL:
SELECT id FROM `table` WHERE column_name LIKE '%keyword%'
I want to fetch all records which has one column contained % sign in mysql
we can do this using mysql using like
for ex..
select * from table where column like '%%';
it returns all records..
Please suggest
Use a backslash to escape the percent:
select * from table where column like '%\%%';
will match any row containing a percent character
% is a special character, try using escape characters to find it. Right now you're just telling mysql to look for a string using 2 wildcard characters (%) as opposed to the actual '%' character. Try using
select * from table where column = 'a%' ESCAPE 'a'
Basically telling mySQL to "Look for the string 'a%', but remove the char a in front of it.
EDIT: Another option is just using
select * from table where column = '\%'
Doing the same thing on later mySQL versions. The backslash is the "standard" escape character.
EDIT 2: Or to actually answer your question:
select * from table where column = '%\%%'
You need to escape the literal % sign with a \ e.g.
select * from table where column like '\%%';