In my MySQL table I have a column named member_id. That column stores values like this:
1,2,3,4,5
I need to check that by using
SELECT *
FROM member
WHERE 5 IN member_id
It's not working well. Please help me write a SQL query that will find the appropriate results.
Use this query:
SELECT *
FROM mytable
WHERE FIND_IN_SET(5, member_id)
Quassnoi's method is preferred for MySQL, but for a portable cross-platform technique, you can do this:
select *
from member
where member_id = '5'
or member_id like '5,%'
or member_id like '%,5,%'
or member_id like '%,5'
This assumes there is no extra whitespace in your data.
SELECT * FROM member WHERE member_id LIKE '%5%'
Related
Name
UID
Late
Tin
ABC
0
Bob
ABC
0
SELECT * FROM `logs` WHERE Name='Tin' AND Name='Feryal'
This query returns nothing for me and only works when I want one name.
I could use the SELECT * but for this case I would like to call specific names in the query?
For this use In clause.
SELECT * FROM logs WHERE Name IN ('Tin', 'Feryal');
You can also use or clause
SELECT * FROM logs WHERE Name='Tin' OR Name='Feryal'
To add on to Amit Verma's answer.
SELECT * FROM `logs` WHERE Name='Tin' AND Name='Feryal'
Reading that SQL statement out loud, it sounds like this:
I want to select all the rows from logs where the name is equal to Tin AND the name is equal to Feryal.
You can quickly see from that statement, the reason why 0 rows are returned, it's because that is impossible! You cannot have somebody named both Tin and Feryal at the same time unless they are some bizarre super-positional being and the datatype in the table somehow allows for that.
Amit covers the rest.
I want to select distinct combination of user age (a column name) and user name (another column name) , but when I write distinct (user_age, user_name), there is syntax error. If anyone have ideas how to write distinct with multiple columns it will be great.
BTW, using MySQL Workbench/MySQL
thanks in advance,
Lin
You have to leave the parenthesis. Just write
SELECT distinct user_age, user_name FROM foo where bar;
;-)
There are 2 ways to achieve this
SELECT DISTINCT user_age, user_name FROM table WHERE some_condition;
OR
SELECT user_age, user_name FROM table WHERE some_condition GROUP BY user_age, user_name;
Hope this helps
I need MySQL select query like
select wt_id,wt_name from work_type where cat_id=1,2,5..;
Is it possible?
Use IN operator ex.
... where cat_id IN (1,2,5..)
You just have to use IN operator
SELECT wt_id, wt_name
FROM work_type
WHERE cat_id IN (1,2,5..);
You can use SQL IN operator.
SELECT wt_id, wt_name
FROM work_type
WHERE cat_id IN (1,2,5..);
If you have any question please comment below.
I have a mysql table contains lot's of records. my table has a varchar field and a timestamp field. (I have one record for every minute)
I want to select records like this:
1,3,5,7,9,11,...
or 1,4,7,10,13,..
or something like this.
I can get done it using php while function, but it is not a good solution. is there any mysql select parameter to get it exactly from mysql?
p.s: sorry for post title, this is the only title stackoverflow accept it.
select * from table where identity_column %2 <>0 -- to select 1,3,5,7,9...
and for your 2 condition do this !
select * from table where identity_column%3 =1 -- to select 1,4,7,10,13,....
For selecting records like 1,3,5,7,9,11,etc. You can do this:
SELECT *
FROM TableName
WHERE autoIncreamentField % 2
NB: Not necessary to check where clause against 0 or 1. It will select records if where clause returns 1. An example in Fiddle.
For records like 1,4,7,10,13,etc. You can do:
SELECT *
FROM TableName
WHERE (autoIncreamentField % 3)=1
select * from table order by rand()
I want to get the exact row from the following data
id name groupid
1 robert 1,2
2 henry 11,12
My query is
SELECT * FROM table WHERE groupid LIKE '%1%'
Above query will return both row
How to get the first row ?
You could use FIND_IN_SET
SELECT * FROM table WHERE FIND_IN_SET('1', groupid)
But as suggestion, you should not save data like this.
assuming groupid is a varchar column having ids stored as comma seperated list you can try this:
select * from table where CONCAT(',',groupid,',') LIKE '%,1,%';
or better approach would be to use FIND_IN_SET function in mysql:
select * from table where find_in_set(1, groupid);
To get an exact row from a database table, just specify all the fields:
SELECT *
FROM myTable
WHERE id = 1
AND name = 'robert'
AND groupid = '1,2'
Or, assuming id is the unique primary key, you can just use that:
SELECT *
FROM myTable
WHERE id = 1
Using LIKE '%1%' will return all the results which contain 1. Use #lc solution WHERE groupid = '1,2' to get only result with that specific id.