I have the table with three fields.
id name place
1 A x
2 A v
3 B z
4 A a
I have the query already like this,
select Id
, Place
from tbname
where Name like '" . mysql_real_escape_string( $name, $sql ) . "'
This will also do wild card matching with '%'
Now what I want is count of distinct values of 'Name' field in the above query without affecting the previous result.
If the wild card matches all the values in 'Name' field,the count is 2.
If I give like,
select Id
, count(distinct Name) as count
, Place from tbname
where Name like '" . mysql_real_escape_string( $tidbit, $sql ) . "'
This will return,
Id:1
count:2
Place:x
But I want all the matches as well as count like,
Id:1,2,3,4
count:2
Place:x,y,z,q
Is there any way to do this in single query or any function is there in sql or php?
Please help me to figure out this problem.
Thanks in advance.
i think
select Res1.id,Res1.cnt,Res2.place from (select id,count(distinct Name) cnt
from tbname groupby id) res1 inner join
select distinct id ,place from tbname group by id ) res2 on
res1.id=res2.id
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.