id | name | permission
1 | John | 1,4,3
2 | mike | 7,4,3
3 | sky | 3,2,1
this is my database
now i have fetch select query with where condition
e.g. select * from friend where permission='4'
but i m not able fetch any data so what to do ?
Please help
Select * from friend where FIND_IN_SET('4',permission);
It looks like you have multiple permissions stored together in a string.
To you, the query looks like:
select * from friend where permission='4'
That means anything containing 4.
To mysql, it sees:
select * from friend where permission= ONLY '4'
// Meaning the permissions column can ONLY CONTAIN 4.
// Also, ONLY is meant as a visual, don't use it in queries.
Try:
find_in_set('4',permission) <> 0
// This means It needs to find number 4 and can't return 0 results.
Check out these for more info:
MySQL query finding values in a comma separated string
http://www.w3resource.com/mysql/string-functions/mysql-find_in_set-function.php
It can be done by the following query :
select * from friend where permission like '%4%'
Related
I have a table like this
d_id | d_name | d_desc | sid
1 |flu | .... |4,13,19
Where sid is VARCHAR. What i want to do is when enter 4 or 13 or 19, it will display flu. However my query only works when user select all those value. Here is my query
SELECT * FROM diseases where sid LIKE '%sid1++%'
From above query, I work with PHP and use for loop to put the sid value inside LIKE value. So there I just put sid++ to keep it simple. My query only works when all of the value is present. If let say user select 4 and 19 which will be '%4,19%' then it display nothing. Thanks all.
If you must do what you ask for, you can try to use FIND_IN_SET().
SELECT d_id, d_name, d_description
FROM diseases
WHERE FIND_IN_SET(13,sid)<>0
But this query will not be sargable, so it will be outrageously slow if your table contains more than a few dozen rows. And the ICD10 list of disease codes contains almost 92,000 rows. You don't want your patient to die or get well before you finish looking up her disease. :-)
So, you should create a separate table. Let's call it diseases_sid.
It will contain two columns. For your example the contents will be
d_id sid
1 4
1 13
1 19
If you want to find a row from your diseases table by sid, do this.
SELECT d.d_id, d.d_name, d.d_description
FROM diseases d
JOIN diseases_sid ds ON d.d_id = ds.d_id
WHERE ds.sid = 13
That's what my colleagues are talking about in the comments when they mention normalization.
Table
Hello i try to finds all hashtags with name #c3 and mysqli query show me: 3 rows but correct is 2 rows with #c3 ??? where is problem ? I want to show me 2Rows !
$sql_query = mysqli_query($Connection, "SELECT * FROM my_table WHERE hashtag LIKE '%#c3%'");
echo mysqli_num_rows($sql_query);
To answer this and as left in comments (by myself)
Use '%#c3' by removing the last %
Using LIKE, the % wildcard does the following and a few examples:
%XXX% - Matches anything before and after XXX
%XXX - Matches anything before XXX
XXX% - Matches anything after XXX.
References:
http://dev.mysql.com/doc/refman/5.7/en/string-comparison-functions.html
http://dev.mysql.com/doc/refman/5.7/en/pattern-matching.html
http://www.tutorialspoint.com/mysql/mysql-like-clause.htm
Plus, should there be any user input, consider using a prepared statement:
https://en.wikipedia.org/wiki/Prepared_statement
It will help against a potential SQL injection.
https://en.wikipedia.org/wiki/SQL_injection
Hi Your query is wrong try to use this
SELECT * from my_table WHERE FIND_IN_SET('#c3', hashtag);
FIND_IN_SET returns the position of a string if it is present (as a substring) within a list of strings. The string list itself is a string contains substrings separated by ‘,’ (comma) character.
for more info read http://www.w3resource.com/mysql/string-functions/mysql-find_in_set-function.php
You can use FIND_IN_SET
SELECT * FROM my_table WHERE FIND_IN_SET('#c3',hashtag)
See docs here and example here
Split the table into 2 tables:
post: id | text | date
1
2
3
post_hashtag: post_id | hashtag
1 #c1
1 #c3
2 #c3
3 #c3blabla
Then your query becomes:
SELECT post.*,post_hashtag.hashtag FROM post JOIN post_hashtag ON post.id=post_hashtag.post_id WHERE hashtag='#c3#';
The act of splitting the table is called normalizing and you need to do this because your tables are not in any normal form which makes them not relational which defeats the purpose of storing them in a relational database. Check https://en.wikipedia.org/wiki/First_normal_form for more information.
Looking at your table, I would suggest that you break up your tables to have a many to many link for hashtag. That way you can search for all records that have a matching hashtag.
data table
+----+------+------+
| id | text | date |
+----+------+------+
hash table
+----+------+
| id | hash |
+----+------+
link table
+---------+---------+
| data_id | hash_id |
+---------+---------+
This would then allow you to use an SQL statement like:
SELECT * FROM data
INNER JOIN link ON data_id = data.id
INNER JOIN hash ON hash_id = hash.id
WHERE hash = '#3';
I have a small problem with a php mysql query, I am looking for help.
I have a family tree table, where I am storing for each person his/her ancestors id separated by a comma. like so
id ancestors
10 1,3,4,5
So the person of id 10 is fathered by id 5 who is fathered by id 4 who is fathered by 3 etc...
Now I wish to select all the people who have id x in their ancestors, so the query will be something like:
select * from people where ancestors like '%x%'
Now this would work fine except, if id x is lets say 2, and a record has an ancestor id 32, this like query will retrieve 32 because 32 contains 2. And if I use '%,x,%' (include commas) the query will ignore the records whose ancestor x is on either edge(left or right) of the column. It will also ignore the records whose x is the only ancestor since no commas are present.
So in short, I need a like query that looks up an expression that either is surrounded by commas or not surrounded by anything. Or a query that gets the regular expression provided that no numbers are around. And I need it as efficient as possible (I suck at writing regular expressions)
Thank you.
Edit: Okay guys, help me come up with a better schema.
You are not storing your data in a proper way. Anyway, if you still want to use this schema you should use FIND_IN_SET instead of LIKE to avoid undesired results.
SELECT *
FROM mytable
WHERE FIND_IN_SET(2, ancestors) <> 0
You should consider redesigning your database structure. Add new table "ancestors" to database with columns:
id id_person ancestor
1 10 1
2 10 3
3 10 4
After -- use JOIN query with "WHERE IN" to choose right rows.
You're having this issue because of wrong design of database.First DBMS based db's aren't meant for this kind of data,graph based db's are more likely to fit for this kind of solution.
if it contain small amount of data you could use mysql but still the design is still wrong,if you only care about their 'father' then just add a column to person (or what ever you call it) table. if its null - has no father/unknown otherwise - contains (int) of his parent.
In case you need more then just 'father' relationship you could use a pivot table to contain two persons relationship but thats not a simple task to do.
There are a few established ways of storing hierarchical data in RDBMS. I've found this slideshow to be very helpful in the past:
Models for Hierarchical Design
Since the data deals with ancestry - and therefore you wouldn't expect it to change that often - a closure table could fit the bill.
Whatever model you choose, be sure to look around and see if someone else has already implemented it.
You could store your values as a JSON Array
id | ancestors
10 | {"1","3","4","5"}
and then query as follows:
$query = 'select * from people where ancestors like \'%"x"%\'';
Better is of course using a mapping table for your many-to-many relation
You can do this with regexp:
SELECT * FROM mytable WHERE name REGEXP ',?(x),?'
where x is your searched value
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,ancestors VARCHAR(250) NOT NULL
);
INSERT INTO my_table VALUES(10,',1,3,4,5');
SELECT *
FROM my_table
WHERE CONCAT(ancestors,',') LIKE '%,5,%';
+----+-----------+
| id | ancestors |
+----+-----------+
| 10 | ,1,3,4,5 |
+----+-----------+
SELECT *
FROM my_table
WHERE CONCAT(ancestors,',') LIKE '%,4,%';
+----+-----------+
| id | ancestors |
+----+-----------+
| 10 | ,1,3,4,5 |
+----+-----------+
please help me with mysql query.
id name manual_id
------------------------------
1 windows 1,2
2 apple 1
3 linux 11,2
Consider above table. I want to fetch only records who contains 1
so as per above table it should only fetch id 1 and 2 that contains "1". it should not get 11 as a result.
I tried using LIKE but it fetches manual_id 11 as well.
Give me exact way to fetch this record.
Thanks in advance.
Use following query
SELECT * FROM `users`
WHERE
manual_id LIKE '%,1,%' OR
manual_id LIKE '1,%'
OR manual_id LIKE '%,1' OR
IF(manual_id = '1' AND CHAR_LENGTH(manual_id) = 1, true,'Invalid record')
Explanation : Here it will search for record with manual id LIKE '%,1,%' means Comma after AND before 1 . It will also check for '1,%,' it would be 1st number in that field . It will check for '%,1' too means last 1 .
The last if condition is IF -> there is 1 only in record.
Working SQLFiddle
Use IN
SELECT * from `table`
WHERE `manual_id` IN (1);
SELECT * from `table`
WHERE FIND_IN_SET('1',`manual_id`)
Hi i have a query which will result three rows of results at present. I need to show them in some way but i am not able to show like that. Can you give me some idea how i need to approach it.
I will have all the result set in an array from database.
1. client name |RO NO | channel
2. a | abc | x
3. a | abc | y
4. a | abc | z
Result set look like above but i need to display it like below in a table
1. client a
2. RO abc
3. Channel x,y,z
I don't want repeat which are common in each row. How should i do it. Can any one give me idea how to proceed...
You can use group_concat:
select `client name` as `client`, `RO NO` as `RO`, group_concat(channel) as `Channel`
from table_name
group by `client`
PHP
Use the result as array key:
$arr[$result] = true;
echo implode(',', array_keys($arr));
MySQL
Group concat with distinct:
select group_concat(distinct ...) from ...;