I have this table under user_name='high'
function_description :
akram is in a date
test
akram is studying
test4
kheith is male
test3
I want a query that returns results of field that have at least an 'akram'
SELECT *
FROM functions
WHERE 'isEnabled'=1
AND 'isPrivate'=1
AND user_name='high'
AND function_description LIKE '%akram%'
and this returns absolutely nothing!
Why?
You are listing the column names as if they are strings. This is why it returns nothing.
Try this:
SELECT *
FROM functions
WHERE user_name='high'
AND function_description LIKE '%akram%'
edit: After trying to re-read your question... are isEnabled and isPrivate columns in this table?
edit2: updated.. remove those unknown columns.
You are comparing strings 'isEnabled' with integer 1, which likely leads to the integer being converted to a string, and the comparison then fails. (The alternative is that the string is converted to an integer 0 and the comparison still fails.)
In MySQL, you use back-quotes, not single quotes, to quote column and table names:
SELECT *
FROM `functions`
WHERE `isEnabled` = 1
AND `isPrivate` = 1
AND `user_name` = 'high'
AND `function_description` LIKE '%akram%'
In standard SQL, you use double quotes to create a 'delimited identifier'; in Microsoft SQL Server, you use square brackets around the names.
Please show the schema more carefully (column names, sample values, types if need be) next time.
Related
I am trying to write a Query to find if a string contains part of the value in Column (Not to confuse with the query to find if a column contains part of a string).
Say for example I have a column in a table with values
ABC,XYZ
If I give search string
ABCDEFG
then I want the row with ABC to be displayed.
If my search string is XYZDSDS then the row with value XYZ should be displayed
The answer would be "use LIKE".
See the documentation: https://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html
You can do WHERE 'string' LIKE CONCAT(column , '%')
Thus the query becomes:
select * from t1 where 'ABCDEFG' LIKE CONCAT(column1,'%');
If you need to match anywhere in the string:
select * from t1 where 'ABCDEFG' LIKE CONCAT('%',column1,'%');
Here you can see it working in a fiddle:
http://sqlfiddle.com/#!9/d1596/4
Select * from table where #param like '%' + col + '%'
First, you appear to be storing lists of things in a column. This is the wrong approach to storing values in the database. You should have a junction table, with one row per entity and value -- that is, a separate row for ABC and XYZ in your example. SQL has a great data structure for storing lists. It is called a "table", not a "string".
If you are stuck with such a format and using MySQL, there is a function that can help:
where find_in_set('ABC', col)
MySQL treats a comma delimited string as a "set" and offers this function. However, this function cannot use indexes, so it is not particularly efficient. Did I mention that you should use a junction table instead?
I have a table with three columns: id, first, and last.
I'd like to find all records where 'SAM' is in either the first or last field.
I'm not an expert with MySQL, but it seems to me that one field could be queried using the LIKE operator.
How can I use LIKE in a query to get data from both columns at the same time?
I've tried this:
SELECT id
FROM `employees`
WHERE 'first' like 'SAM' OR 'last' like 'SAM'
But I get the message "You have an error in your SQL syntax; Check the manual that corresponds to your MySQL server version for the right syntax."
I can't seem to figure out why you are getting a syntax error, but one problem with your query is that by putting single quotes around your column name causes it to be treated like a string literal instead of a column name.
What I mean by that is instead of comparing the value in the first column with the string 'SAM', it's comparing two strings, 'first' and 'SAM' which are different. This query would return no results.
In addition, this will only work if the first or last name is equal to sam. To check for those characters as a substring at any point, I would add wildcards at the front and back of the strings.
Try this:
SELECT id
FROM employees
WHERE first LIKE '%SAM%' OR last LIKE '%SAM%'
Here is an SQL Fiddle to show how it works.
I don't see any syntax error in the query you posted. What you have is really close.
Your query isn't comparing the contents of the columns; it's comparing string literals, because of the single quotes, those are strings, not column references.
The LIKE comparison is equivalent to an equality comparison, since the there aren't any wildcard characters in the string on the right side. The '%' character is a wildcard that will match any number of characters.
To return rows where either of the columns first or last contain the string "SAM", you could do something like this:
SELECT e.id FROM employees e WHERE e.first LIKE '%SAM%' OR e.last LIKE '%SAM%'
That query would match any of these example rows:
id first last
-- --------- -------
2 Flotsam
3 Samson
5 Sesame
7 Yosemite Sam
I'm trying to change a table field that contains decimal numbers from varchar(255) to decimal(12,2). And before I do that, I'd like to find out if there is information that would get deleted in the process: are there any rows where this field contains something other than a decimal(12,2).
I'm stumped how to do this. Apparently there isn't a string function like is_numeric() in PHP. I already tried casting the field to decimal and then comparing it with the original string, but this returns TRUE even for obvious cases where it should not:
select ('abc' = convert('abc', decimal(12,2)));
returns 1
Any help? How do I find out if a string contains something other than a decimal in MySQL? Thanks.
Stupid me, I have to cast twice (to decimal and back to char), which makes it work:
select ('abc' = convert(convert('abc', decimal(12,2)), char(255)));
returns 0
Thanks.
If you want to examine if the strings are actually floating points numbers, you could also use a regular expression. The following regex can help :)
SELECT '31.23' REGEXP '^[[:digit:]]+([.period.][[:digit:]]+)?$'; # returns 1
SELECT '31' REGEXP '^[[:digit:]]+([.period.][[:digit:]]+)?$'; # returns 1
SELECT 'hey' REGEXP '^[[:digit:]]+([.period.][[:digit:]]+)?$'; # returns 0
I know it is not an appropriate technique to have a structure of MySQL table as such, but I have to work with such. The problem is, that the field in table has value with comma seperated integers, like "1,3,5,7,10" and I want the query to return rows, in which field has a to the query passed number in it, like:
SELECT * FROM `table` WHERE '5' IN (`field_in_table`)
However, it does not work, if, in this case, '5' is not the first number in the field.
Any advises, how to solve that?
Thanks in advance,
Regards,
Jonas
Have a look at
FIND_IN_SET
Returns a value in the range of 1 to N
if the string str is in the string
list strlist consisting of N
substrings. A string list is a string
composed of substrings separated by
“,” characters. If the first argument
is a constant string and the second is
a column of type SET, the
FIND_IN_SET() function is optimized to
use bit arithmetic. Returns 0 if str
is not in strlist or if strlist is the
empty string.
You could use WHERE field_in_table LIKE '%5%' instead.
Of course, the problem would be, '1,59,98' would return as wel.
SELECT * FROM table WHERE field_in_table LIKE '%5'");
should work
You could try
SELECT *
FROM table
WHERE '%,5,%' LIKE field_in_table OR
'%,5' LIKE field_in_table OR
'5,%' LIKE field_in_table;
A better approach might be to use regular expressions, a subject on which I am not an authority.
SELECT *
FROM table
WHERE FIELD LIKE '%,5,%' OR
FIELD LIKE '5,%' OR
FIELD LIKE '%,5'
I have read that after select we use column-names but I have found a statement that was like this:
SELECT 'A' FROM T WHERE A = NULL;
would you lease help me? thanks (A is a column- name here?)
my DBMS is MySQL
EDITED : the exact question is this that:
Will the above statement produce a row (select all that
apply)? Notice that ANSI_NULLS is OFF.
I want to know that the above statement will work? because some of you said that we should write IS NULL instead of =null
Based on that query, you would get a result set containing the character 'A' for each row where the column named A was equal to null.
If you actually want to see the value of the column A instead of the character 'A', you have to remove the single quotes:
SELECT A FROM T WHERE A IS NULL
Either way, you should not use = NULL. Certain RDMSs don't handle that the way you would think. The standard is to use IS NULL instead.
You should use
SELECT 'A' FROM T WHERE A IS NULL;
There are three types of quotes in SQL.
The single quote ' means that something is a string literal. 'A' in this instance means that it returns the character A for all rows where the column A is NULL.
The double quote " means that something is an identifier. This is useful if the identifier has the same name as a reserved word, like select. Example: SELECT "select" FROM T selects the column select from the table T.
The backtick quote ` works only in MySQL, and is the same as the double quote. The double quote can sometimes used for string literals in MySQL, although this is very much against the standard. MySQL has an option to conform to the standard, using SET SQL_MODE='ANSI'; where the backtick becomes invalid, and you need to use the single and double quotes instead.
An identifier without quotes is the same as an identifier with double quotes, unless it's a reserved word.
Hope this helps understand a bit more.
In answer to your question:
A = NULL is always false, so you will get no rows returned. To compare with NULL you must use A is NULL instead.
NULL is special in SQL, in that it is not equal to anything, even itself. Yep, (NULL = NULL) evaluates to false.
If you change it to IS NULL, then you will get a set of rows with one column, containing the character 'A' in each row. You will get one 'A' for each row in the table T where the A column is null.
You will get the letter A and not the value of the column because you have quotes around the 'A'. If you remove them, you'll get the value of A in each row (which will be null, because those are the rows you're selecting with your where clause).
If you wanted to see which rows in T had a null value for A, then you should change it to select * from T where A is null
Your SELECT statement has the following meaning:
"For every row of the table called T, return the string 'A' if the column A of the table T is NULL"
So, if you have 3 records where A is NULL, the output will be:
A
A
A
3 row(s) selected
The correct syntax is WHERE A IS NULL, and not WHERE A = NULL.
Have you tried running it on your test database to see what it does? Or was this just in reading?
Breaking down that statement, what is says is:
In the table T (FROM T), find the rows where the value of A is null (WHERE A = NULL).
For each of those rows, return an 'A'.
The result I would expect is
+--+
|T |
+--+
|A |
|A |
...
|A |
+--+
If the statement was instead:
SELECT A FROM T WHERE A = NULL;
Where the single quotes are removed, it would return a bunch of nulls, the value of the column A.
A is a column name, but you probably don't want single-quotes around it. I'd try...
SELECT A FROM T WHERE A IS NULL;