This seems to be an easy question but I can't for the life of me find an answer. How do I select all rows in a column regardless of value? For instance if I want everything in the column location:
"SELECT * FROM whateverTable WHERE id='$id' AND date='$date' AND location='I don't care, return everything'"
Keep in mind that I need location in there because I'm getting a dynamic URL query to this database, otherwise I would have just left it out.
Edit
I've read some of the answers and shame on me for not making myself clearer. I'm trying to see if a MySQL query where you can select every row in a column is possible without the need for this:
if ($location == '') {
"SELECT * FROM whateverTable WHERE id='$id' AND date='$date'"
} else {
"SELECT * FROM whateverTable WHERE id='$id' AND date='$date' AND location='$location'"
}
Rather than hacking up my code like that (because I have a hell of alot more query clauses in my real code), I'm trying to see if something like this is possible :
"SELECT * FROM whateverTable WHERE id='$id' AND date='$date' AND location='%'"
or this:
"SELECT * FROM whateverTable WHERE id='$id' AND date='$date' AND location='.*'"
But I'm not having any luck. If it's not, I'll just move on. Sorry for not making myself clearer
As you said in your question, you should build the query dynamically and just omit that field. The query would then become:
SELECT * FROM whateverTable
WHERE id = '$id'
AND date = '$date'
If you can't do that for some reason, you could try something like this:
SELECT * FROM whateverTable
WHERE id = '$id'
AND date = '$date'
AND (location='$location' OR '$location' = '')
Setting $location to the empty string will cause all locations to be returned.
And don't forget to correctly escape your strings to avoid SQL injection attacks.
SELECT location FROM whateverTable will return you every single row/value in the location column.
Add your where clause if you want to start filtering the results down.
If you're dynamically building the query just check if location is null or the empty string and then omit that part of the query.
Replace equal symbol : "=" by LIKE
SELECT * FROM whateverTable WHERE id='$id' AND date='$date' AND location LIKE '%'
I think you're looking for a sub query?
SELECT * FROM whateverTable WHERE location IN (SELECT location FROM whateverTable);
Related
MySQL column:
phone = '(999)-666-333'
String to search '999666333'
Is there a way I could search by transforming the value '(999)-666-333' within WHERE command?
I want to strip all non-digits and leave only digits.
something like:
select * from users where regex(phone, '[0-9]') = '999666333';
Something like this works in my case:
SELECT * FROM users WHERE REGEXP_REPLACE(phone, '[^0-9]', '') = '999666333';
PROVINCE -- varchar(25)
SELECT * FROM listings WHERE PROVINCE='Bakersfield'
Returns empty resultset when entries with Bakersfield as province exist
I am pretty unsure as to why. When I remove the WHERE it works.
BTW I am running this in phpmyadmin, where it lets one execute SQL statements
In phpmyadmin it shows ... beneath the equals sign. I am not sure why. That may be a hint.
If
SELECT * FROM listings WHERE PROVINCE like '%Bakersfield%'
works for you then you have spaces in your data. You can remove them with
update listings
set PROVINCE = trim(PROVINCE)
See TRIM()
After that you should check your code where you insert the data. Check why there are spaces inserted and fix it.
Change you equal = to LIKE operator
SELECT * FROM listings WHERE PROVINCE LIKE 'Bakersfield'
This should solve your issue. If you want to find places that includes your string simply surround it with wildcards %..%
SELECT * FROM listings WHERE PROVINCE LIKE '%Bakersfield%'
SELECT * FROM listings WHERE PROVINCE='Bakersfield'
There is no point the abpve query doesnt work if the follwing case is not the problem.
1) If case is the prob then use upper or lower
SELECT * FROM listings WHERE upper(PROVINCE)=upper('Bakersfield');
2) If extra space is the problem use replace or Trim
SELECT * FROM listings WHERE replace(PROVINCE,' ','') ='Bakersfield'
There might be sum other seniario if not then use "Like " key word as suggested by others. :)
I try make search for my site. Example: have field name = city, if city is empty my query looks: SELECT * FROM TABLE WHERE CITY = ALL;
I don't know how to write: CITY = ALL correct, expression CITY is not null will be complicated because I should be remove =
Try this:
SELECT *
FROM Table
WHERE #SearchParam IS NULL OR City = #SearchParam
If his parameter #SearchParam is passed to the query with a NULL value then it will return all the data in the table, otherwise it will search for the cities with this parameter.
remove the where completely
SELECT * FROM TABLE
or
SELECT * FROM TABLE WHERE CITY = #searchstring or #searchstring is null;
If you don't care about very optimized queries, you can use a like operator compare with
City like '%#exp'
If exp is empty the query returns all Cities.
I personally do not recommend this :)
All the best
If you are getting them all, why have the WHERE clause in at all?
SELECT * FROM TABLE
Note, you should avoid using SELECT * whenever possible. As it is a waste of resources, always be explicit with your columns returned.
Maybe something like this?
SELECT * FROM TABLE WHERE (CITY = SOMETHING) OR (SOMETHING is null);
Search for: 'chemist'
Problem: query which will match a string like 'onechemist' but not 'chemist'.
SELECT id,name FROM `records`
WHERE name LIKE '%". mysql_real_escape_string($q) ."%'
This alternate try won't work:
SELECT id,name FROM `records`
WHERE name LIKE '%". mysql_real_escape_string($q) ."%'
OR name LIKE '". mysql_real_escape_string($q) ."%'
OR name LIKE '%". mysql_real_escape_string($q) ."'
How could I compile the above into one single query that will match any field which has the string or optimize the query into a better expression?
If $q is holding 'chemist', it will match a name that is also 'chemist`. In that case, your first query should work. Try double checking your values.
$sql = "SELECT `id`, `name` FROM `records`
WHERE `name` LIKE '%".mysql_real_escape_string($q)."%'";
PS - Your 2nd query will pull the same results as your 1st.
You can use regular expression for this. for example,
SELECT id,name FROM `records` where name REGEXP '^onechemist'
It will match names starts with onechemist
I've a problem and I'm guessing if there's a better way to achieve my goal. I've this query in Mysql to retrieve some rows:
SELECT *
FROM table
WHERE field IN ('V','S','G','B')
What I would like to do is to run a query that retrieve the rows where the field has value LIKE those in the IN list. I know that the trivial way is to use this query:
SELECT *
FROM table
WHERE field LIKE '%V%' OR field LIKE '%S%' OR
field LIKE '%G%' OR field LIKE '%B%'
What I want to know is there's an operator that do this or at least, if that operator does not exist, a better way to write the query.
Thanks to everyone that will help me.
Put the values in a table (Params) then use
SELECT *
FROM table
WHERE EXISTS (
SELECT *
FROM Params
WHERE table.field LIKE '%' + Params.col + '%'
);
Consider also putting the wildcard characters into the values in the Params table.