So,
SELECT * FROM table WHERE col LIKE '%'
will return everything. Is there a wildcard for the query
SELECT * FROM table WHERE col = '*'
Clearly * doesn't work, I just put it there to indicate where I'd like a wildcard. The column I'm selecting from contains an integer between 1 and 12, and I want to be able to select either all records with a particular number, or all records with a wildcard.
Thanks,
LIKE is basically the same as =, except LIKE lets you use wildcards.
These two queries will return the same results:
SELECT * FROM table WHERE col LIKE 'xyz';
SELECT * FROM table WHERE col='xyz';
Without a '%' in the LIKE query, it is effectively the same as '='.
If you're doing a selection on an integer column, you should consider using the IN() or BETWEEN operators. It sounds like you have two separate conditions that should be handled in your code however, rather than in the query, as your conditions dictate that you need at least two different kinds of queries.
Edit: I should clarify that LIKE and = are similar only in normal, humdrum string comparison usage. You should check the MySQL Manual for specifics on how it works, as there are situations where it's not the same (such as language sets).
If you want to select everything, why are you attaching the WHERE clause at all? Just leave it off conditionally instead of putting a wildcard into it.
The reason for using LIKE is because the = does not offer wildcard support. Otherwise there would be no reason for LIKE
SELECT * FROM table WHERE col RLIKE '.*'
i.e. regular-expression LIKE.
zombat's answer is great, but I only noticed in his answer that you are selecting integers. He mentioned IN() and BETWEEN(). Here's examples using those syntaxes, as well as some other options you have for an integer field.
SELECT * FROM table WHERE col = 1;
SELECT * FROM table WHERE col BETWEEN 1 AND 12;
SELECT * FROM table WHERE col BETWEEN 6 AND 12;
SELECT * FROM table WHERE col <= 6;
SELECT * FROM table WHERE col < 6;
SELECT * FROM table WHERE col >= 6;
SELECT * FROM table WHERE col > 6;
SELECT * FROM table WHERE col <> 6;
SELECT * FROM table WHERE col IN (1,2,5,6,10);
SELECT * FROM table WHERE col NOT IN (1,2,5,6,10);
Assuming your query is parameter driven a case statement is probably appropriate
select * from mytable
where col like case when #myvariable is null then % else myvariable end
Where #myvariable is either null if you dont want a value otherwise it would use the integer value you pass in.
I have encountered such a case while building a stored procedure for a report
Following is my solution, hope this is what you had in mind :)
set #p = "ALL";
Query:
select * from fact_orders
where
dim_country_id = if(#p is null or #p="ALL", dim_country_id, #p)
limit 10
;
If your values are in the the range (1,12) then:
select * from table where col>=5 and col<=5; //this is equal to col=5
select * from table where col>=0 and col<=12; //this is equal to col=any value
The same line can produce both effects by choosing the 2 parameters appropriately.
I faced a similar problem when I needed a single prepared statement which should work with 2 different ways , either checking for a particular value in a column or ignoring that column completely.
Related
I run a SQL query like below in MySQL:
select *
from (
select 2 as o,1 as t from dual
union
select 1 as o,2 as t from dual
) x
order by if((select 1),o,t);
It works well, but when I use column relative position in if statement, it doesn't work.
How can I use column relative position in if in ORDER BY statement?
select *
from (
select 2 as o,1 as t from dual
union
select 1 as o,2 as t from dual
) x
order by if((select 0),1,2);
I'm not sure what your real confusion is. When an integer appears in an order by, then this is treated as a column number. Any other use of an integer is interpreted as an expression.
The use of column numbers has been removed from the SQL standard. Hence, its use in any particular database is not guaranteed in future releases. It is really better to use the column names.
I think you want to sort your query based on a criteria over two columns, if I'm correct, you can use something like this:
...
order by
case when (your criteria)
then column1
else column2
end;
Note: use union all instead union when you don't want to remove duplicate values as performance issue ;).
I'm having an issue thinking how to write the query i need.
I'll give you an example of what i'm trying to do..
SELECT * FROM table WHERE clause1=10 AND clause2=20 AND clause3=30||40
I need to select entries where clause1 must be exactly 10 and clause2 must be exactly 20, but clause3 can be 30 or 40 but have to be exactly 30 or exactly 40.
so it would select the entry if it were 10,20,30 or 10,20,40
SELECT * FROM table WHERE clause1=10 AND clause2=20 AND clause3=30 OR clause3=40 is incorrect though isn't it.
The numbers and clauses are only for example.
Thanks.
You've got to use parentheses 'round your last condition and use two times clause3=:
SELECT * FROM table WHERE clause1=10 AND clause2=20 AND (clause3=30 || clause3=40)
or
SELECT * FROM table WHERE clause1=10 AND clause2=20 AND clause3 IN (30, 40)
Note
I would recommend to use the operator OR instead of ||, because in other SQL dialects (i.e. Oracle, Postgres) || is the concatenation operator. OR is standard SQL. So the first statement would better be written:
SELECT * FROM table WHERE clause1=10 AND clause2=20 AND (clause3=30 OR clause3=40)
Explanation
The operator AND has got higher precedence than OR. Without parentheses the expression the following two statements would be evaluated the same
SELECT * FROM table WHERE clause1=10 AND clause2=20 AND clause3=30 OR clause3=40
SELECT * FROM table WHERE (clause1=10 AND clause2=20 AND clause3=30) OR clause3=40
and that is not what you want. So either put parentheses around your OR expressions or use the simpler IN().
Use IN.
SELECT *
FROM table
WHERE clause1=10 AND clause2=20 AND
clause3 in (30,40)
Is there a character, say, $,
SELECT * FROM Persons WHERE firstName='Peter' AND areaCode=$;
such that the statement would return the same as
SELECT * FROM Persons WHERE firstName='Peter'
i.e. areaCode=$ would always return always true and, thus, effectively “turns of” the criteria areaCode=...
I’m writing a VBA code in Excel that fetches some rows based on a number of criteria. The criteria can either be enabled or disabled. A character like $ would make the disabling so much easier.
instead of disabling it, pass it through to your query as NULL and use COALESCE:
SELECT *
FROM Persons
WHERE firstName='Peter'
AND areaCode = COALESCE(<your parameter>, areaCode);
%
See Wildcards
You could use NULL for this purpose:
AND (areaCode = ? OR ? IS NULL)
I think you could use something like
SELECT * FROM Persons WHERE firstName=firstName
of course without quotes
From your question I assume that you actually want the ability to include or exclude the where clause, in which case you need to use or.
SELECT *
FROM Persons
WHERE ( 1 = 2
OR ( firstName = 'Peter'
AND < more conditions if needed >
)
)
In this example 1 <> 2 so the only condition evaluated is firstName = 'Peter'. If you then want to ignore the where clause you change 2 to 1. As 1 = 1 this is evaluated for every row and the rest of the conditions will be ignored.
I am trying to query a table in mysql based on the length of a string in a specific column. I know mysql has a function called LENGTH(), but that returns the length of the string. I want to be able to pull data based on the result of the LENGTH() function.
Example:
SELECT * table WHERE LENGTH(word) = 6
of course that does not work. I read through http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function%5Flength but could not find anything to help me.
yes I could make something in PhP to accomplish this, but I would like to do it at the query level.
Any help?
Try:
SELECT *
FROM table
WHERE LENGTH(RTRIM(word)) = 6
I believe you wanted to use query SELECT * FROM tableName WHERE LENGTH(word) = 6; (assuming that the word is name of column in tableName).
This is very unfortunate solution on large tables, you should create new column and use UPDATE tableName SET wordLength = LENGTH( word).
So I have a data with format like ;1;;2; and then I need to use this number in a query so I thought I'd convert it to 1,2 and use that in a IN condition. In my table, the result should return 2 rows but instead it is returning only 1 row.
My query is like this. The subquery return 1,2 with no problem but only 1 row is retrieve.
select *
from wt_lists
where id IN ((select replace (replace(sendto, ';;',','),';','')
from wt_stats where statsid IN (1)))
But when I try it with this. It returns the correct result, which in my case is 2 rows.
select *
from wt_lists
where id IN (1,2)
What am I missing here?
Comma delimited strings need to be explicitly defined in the query in order to be used in the IN clause - there's countless examples on SO where people need to use dynamic SQL to incorporate user submitted comma delimited strings.
That said, I have a solution using the FIND_IN_SET function:
SELECT DISTINCT wl.*
FROM WT_LISTS wl
JOIN (SELECT REPLACE(REPLACE(ws.sendto, ';;',','),';','') AS ids
FROM WT_STATS ws
WHERE ws.statsid = 1) x ON FIND_IN_SET(wl.id, x.ids) > 0
You are replacing the string:
';1;;2;'
To:
'1,2'
So, you SQL query looks like:
select * from wt_lists where id IN ('1,2') from wt_stats where statsid IN (1)
To use IN clause you need select different values in different rows.
I found this store procedure that does exactly what you need.
http://kedar.nitty-witty.com/blog/mysql-stored-procedure-split-delimited-string-into-rows/
I have not tested, but it is the way.
Obs: Like David said in the comments above, parsing the data in your application is a better way to do this.