I know WHERE, LIKE, IN filters in database. But how can I filter with "except" ? I want to select all data except a specificdata.
Thanks in advance
with NOT IN, usage is like IN
SELECT * FROM USERS WHERE ID NOT IN (1,2,3,4,5)
Three approaches:
WHERE column <> 'something'
WHERE column NOT LIKE 'something'
WHERE column not in('something')
you can use NOT IN
Note that you can use another query for NOT IN like this:
SELECT id FROM customers WHERE id NOT IN (SELECT id FROM bad_customers)
WHERE NOT LIKE
WHERE NOT IN
Related
How to select data from two columns in same table in a single query.
for example select name "waseem" from username if not found check in city row.
select * from <table_name> where name like 'waseem' or city like 'waseem';
I agree with other's answers but with slight modification. If there is a record waseem ahemed and your only looking for waseem wont be searched in other answered query.
You need to use the wildcard % if you want to search for waseem.
I highly suggest you not to use * for selecting the column names because of the performance issues.
SELECT name, username, password WHERE name LIKE '%waseem%' OR city LIKE '%waseem%'
This is working for me
SELECT * FROM table WHERE row_name1 LIKE 'value' OR row_name2 LIKE 'value'
I have data along with the ids 1,2,3,4,5 but these are not primary ids...
I want to fetch data in order like 4,3,2,1,5.
I cann't change the ids in database.If i use order by desc it will fetch 5,4,3,2,1.
you can use FIELD in order clause
order by FIELD(field_name,4,3,2,1,5)
Order by field is one such way:
select * from TABLE order by FIELD(column_name,4,3,2,1,5) ;
You can look into this for more details:
Field Example
you can use ORDER BY RAND() for random order.
Try this,
SELECT table.* FROM table ORDER BY FIELD(id,4,3,2,1,5);
I am hoping to run a mysql_query where I can select a row if a certain field ends in a number. For example:
<?php
$query = mysql_query("SELECT id FROM table WHERE [ID ENDS IN 0]");
$query = mysql_query("SELECT id FROM table WHERE [ID ENDS IN 1]");
$query = mysql_query("SELECT id FROM table WHERE [ID ENDS IN 2]");
$query = mysql_query("SELECT id FROM table WHERE [ID ENDS IN 3]");
//and so on...
?>
Is there a way to do this? Any help would be great. Thank you!
SELECT ...
WHERE somefield LIKE '%1'
SELECT id FROM table WHERE mod(id, 10) = 1
give it a go
SELECT id
FROM table
WHERE id LIKE '%0'
You can use regular expressions if you need to find if field is ending in a number or not as follows
SELECT id FROM table WHERE id REGEXP '[0-9]$'
Hope it helps...
You could do it with a cast and LIKE, but the performance is likely to be terrible for any non-trivial amount of data (I've not tested in your particular case, but in my experience, casting to string so you can use string operations really slows a query down).
A better way would be to use modulus.
For example, to get all the rows where the numerical field ends in a 4:
SELECT *
FROM table
WHERE MOD(column_of_interest, 10) = 4
Again, I've not benchmarked this, but it's probably going to perform better than casting would.
Of course, if the column type is already a string, then LIKE is the way to go, as using MOD on strings would also require a cast.
You can use LIKE and a wild card expression
it should be somthing like
SELECT id FROM table WHERE id REGEX '%0$'
SELECT id FROM table WHERE id REGEX '%1$'
and so on.
SELECT id FROM table WHERE id REGEXP '1[[:>:]]'
I am building a web app that uses a system similar to twitter (Follow someone and that users ID is added to your profile and when you view your timeline you see all of your follows). I am trying to select multiple fields of data in one statement with different conditions. i.e. SELECT * FROM TableName WHERE id='I want to have multiple IDs for the query to select.'. Each user is different so I am wondering do I just separate them by commas or something? I have no idea. Thanks in Advance.
If I understand what you mean correctly, you're looking for the IN keyword that will match any listed id;
SELECT * FROM TableName WHERE id IN (1,7,12,44,36);
Use "WHERE id IN (1, 2, 3)"
For example.
You can use IN LIST function like SELECT * FROM TableName WHERE id IN (1,2,3,4,.....)
as stated, you are looking for the IN clause... however, I think this is really a JOIN to your 'friends' table instead.
SELECT *
FROM tableName
WHERE ID IN (1,2,3, ...)
I am running a query like this
SELECT ....... WHERE column_name IN (1,2,3)
How can I use the same query and say
SELECT ........WHERE column_name IN (*)
and select all rows?
Edit: The reason I need to do this is because of a bad database design and bad coding in general, for now I need to hack it. If I don't pass the query 1,2,3 .. I need to pass it something so it will return me all records. Is there a way to do it without changing the query? If you don't think this is possible then you should say that it is not possible in your answer.
Just remove the WHERE clause alltogether.
If you absolutely must have the WHERE clause, you could say:
WHERE 1
Or if you need to do the column_name IN bit, you could do something like:
SELECT ....... WHERE column_name IN (1,2,3) OR 1
... IN (SELECT DISTINCT column_name FROM table) ...
If you are trying to filter your return table base on values in another table:
select column_1, column_2
from table_1
where column_1 IN (select column_with_value from tabl_2)