In my mysql table one of the field has values like (1|2|3) stored in it.
id Skills
---- --------
1 1|2|3|5
2 2|4|5
3 4|6|3
4 5|2|3|1
I want to search and list the id's based on the skills matched. If i want to list the id's of the skill which contains 3, i have to list the ids 1,3,4. Like if i want to list the id's of the skill which contains 1,5 (either 1 nor 5 or both)(like mulitple values 1,4,3) then i want to list 1,2,4(i want to list 2 also). Any help could be greatly appreciated.
Thanks in advance.
Use FIND_IN_SET
select id
from your_table
where find_in_set('3', replace(skills, '|',',') > 0
select id
from your_table
where find_in_set('3', replace(skills, '|',',')) > 0
or find_in_set('5', replace(skills, '|',',')) > 0
But you should actually change your DB structure. Always store single values in a column to avoid such problems!
This may help:
SELECT * FROM YOUR_TABLE
WHERE Skills REGEXP '[[:<:]]1[[:>:]]' = 1
OR Skills REGEXP '[[:<:]]5[[:>:]]' = 1
Related
I have comma separated value in mysql table field and want to get most common value amongst them.
i.e. I have a table name is A, In which there are two fields id and available_values.
id available_values
--- -----------------
1 3,5,7,9
2 3,5
3 5,9
In above example there are value 5 exist in all rows so i need to get this value(mean - 5), because this is available in every records.
Please help to find out the solution.
Thanks in advance.
Best way is to include another table with one row per id and your values. But you can use REGEXP to achieve the output :
SELECT * FROM table_name WHERE
available_values REGEXP CONCAT('(^|,)(', replace(5,',','|'),')(,|$)')
SELECT * FROM `test` WHERE FIND_IN_SET($keysValue,available_values)
example
SELECT * FROM `tableName` WHERE FIND_IN_SET($keysValue,columnsName)
I have a table, named group, in mysql. One of its field is member_ids where values are like (2,3,4,5).
I want to use 2,4,5 excluding 3 in one place and 3 excluding others in another.
I am using mysql as a database for PHP. Any idea how to do it with mysql query or php code?
member_ids: (2,3,4), (2,3,4,5), (1,3,5,7) ...
There are multiple values in single cell.
Now I want to use all values of 2nd cell except 3 i.e. (2,4,5) to find email_id from another table member.
I can only use '3' in the query, not the rest i.e. (2,4,5).
I need to find email id of 'id'= 2, 4 and 5 from member table.
To get all member_ids whose value is excluding 3 , you can write
select member_ids from group where member_ids != 3;
to get all member_ids whose value is 3 excluding others, you can write
select member_ids from group where member_ids = 3;
You can use FIND_IN_SET() like this:
SELECT g.member_ids
FROM group g
WHERE FIND_IN_SET('2', g.member_ids) > 0
OR FIND_IN_SET('4', g.member_ids) > 0
OR FIND_IN_SET('5', g.member_ids) > 0
I am trying to select from a database but because the database as duplicate data and the item names in each data may or may not be duplicated. Please look at the example below to understand more
Table shoe
shoeid pid Name
1 1 green
2 1 green
3 2 red
4 3 red
Thats a simple example.
How I can I select Name and pid from this table but i dont want to see any repeated numbers or names. for example i don't want to see red or green or whatever color i have in the database more than once. Please remember I have over 100 colors in the database. The same thing apply to the pid
Use DISTINCT
http://www.mysqltutorial.org/mysql-distinct.aspx
http://www.w3schools.com/sql/sql_distinct.asp
This might be what you want
this gives you only unique results
SELECT DISTINCT Name, pid FROM shoe;
try this:
SELECT DISTINCT Name FROM table_name;
or if you want maximum id in output:
SELECT Name, MAX(pid) AS id FROM table_name GROUP BY Name;
or if you want comma separated list of ids for that Name:
SELECT Name, GROUP_CONCAT(pid) AS id_list FROM table_name GROUP BY Name;
Use GROUP BY
select * from `yourtable` group by `pid`,`Name`
SELECT DISTINCT Name FROM shoe
this query get the unique values , if you want where you can use it by
SELECT DISTINCT Name FROM shoe WHERE your_key = 'your_key_val'
hi I'm looking for a way to only show a matching set of mysql results only once. can anyone tell me how to do this?
here's an example of what i'm trying to achieve:
id | profile_id | viewed_profile_id | date_viewed
1 4 7 00:00:00
2 5 6 00:00:00
1 4 7 00:00:00
so if profile_id and viewed_profile_id match then to only show one result for those matching columns rather than twice or three times or however many times it appears in the database?
Use the DISTINCT keyword:
SELECT DISTINCT id, profile_id, viewed_profile_id, date_viewed
FROM myTable
This will show only one row for each unique combination of the columns selected.
Or, reading into your question a lot (since you only want to match profile_id and viewed_profile_id), if you want to show the latest date viewed for each viewer, you can use GROUP BY and select the MAX date viewed. I am also assuming there is data in date_viewed and it is sortable:
SELECT profile_id, viewed_profile_id, MAX(date_viewed)
FROM myTable
GROUP BY profile_id, viewed_profile_id
DISTINCT helps to eliminate duplicates. If a query returns a result that contains duplicate rows, you can remove duplicates to produce a result set in which every row is unique. To do this, include the keyword DISTINCT after SELECT and before the output column list
http://dev.mysql.com/doc/refman/5.0/en/distinct-optimization.html
SELECT DISTINCT(` profile_id`),`viewed_profile_id`,`id`,`date_viewed` FROM `tableName` GROUP BY `viewed_profile_id`
try this:
$query = "SELECT * FROM TABLENAME WHERE profile_id = '$PROFILEIDVALUE' AND viewed_profile_id = '$VIEWEDIDVALUE' LIMIT 0, 1"
Depending upon the ORDER you want use can use ORDER BY id DESC/ASC
I hope this would be useful
for example i have a table like this :
name rating
matei 124
andrei 20
serj 25
john 190
mike 96
andy 245
tom 73
i need to output something like this(order by rating):
john's position is 2; or, tom's position is 5; (i don't need to get all result , just one )
How can I achieve this?
Thanks in advance
Generally order of rows in a query result is not guaranteed by MySQL unless ordering is explicitly specified with ORDER BY clause. If you have some separate ordering column, you may use query like the following:
SELECT count(1) as position
FROM table
WHERE order_column <= {john's order_column value};
If you don't have ordering column, I'd recommend you to define first, what does "john's position" and "tom's position" mean.
UPDATE:
AFAIU, you want to get position in list sorted by rating (sorry, I initially did not get it). So, rating would be your order_column. In this case, you should decide, how do you calculate position, if two guys have equal rating (who's position is higher?).
So, the query may look in the following way:
SELECT count(1) as position
FROM table
WHERE
rating > (SELECT rating FROM table WHERE id={user's ID});
SELECT COUNT(*) + 1
FROM users
WHERE (rating, name) <
(
SELECT rating, name
FROM users
WHERE name = 'john'
)
Note that if you will have duplicates on both name and rating, this query will assign the same rating to both of them.
Tables are more formally known as relations in database literature - they are not guaranteed to be ordered (they are sets of "tuples"), so your question doesn't make sense. If you need to rely on an order/position, you need to define an additional column (like an auto-incrementing ID column) to capture and store that info.
Is this any help > http://craftycodeblog.com/2010/09/13/rownum-simulation-with-mysql/ ?
Would offset not work like so?
SELECT * FROM Table ORDER BY rating DESC LIMIT 1,6
This would return 1 row that has been off setted by 6 rows ? or am I mistaken, the syntax would be
SELECT * FROM Table ORDER BY rating DESC LIMIT 1 , {{POS}}