I have a Database table in MYSQL, it looks like this:
Project_ID user_ID name
11 1 fred
11 2 rick
11 1 fred
I want to get the names in the table, but when I display it I get the same name twice because the user_ID 1 appears twice in the table. I tried to display it with GROUP BY and SUM, but I don't want to do it this way. How do I get it not to return duplicate rows?
Use DISTINCT
SELECT DISTINCT user_ID
, verschil_ma
FROM project_uren
WHERE project_ID = 11
GROUP BY user_ID
Point 1 is that should the user be assigned to the project twice?
Point 2 - Use the DISTINCT keyword to return only unique records - http://dev.mysql.com/doc/refman/5.0/en/distinct-optimization.html
SELECT DISTINCT user_ID
FROM TABLE
WHERE Project_id = 11
That will return you 1 and 2 (You won't get 1 twice)
Thanks
$results = // query
$results = array_unique($results);
Related
I have a table which stores user items, the two key columns which I would like to use in this query are user_id and item_id. The id field in the example is not needed but just added to show these aren't the only two columns in the table.
----------------------
id user_id item_id
----------------------
1 1 324
2 1 324
3 3 324
4 2 230
5 4 324
The query which I would like to construct should return the top 10 users who have the most items with a specific item id.
So for example if I wanted to run the query against the item ID 324 I should get the following result.
-------------------
user_id item_count
-------------------
1 2
3 1
4 1
2 0
try this
select user_id , count(*) as item_count from table
where item_id = 324 group by user_id order by item_count desc limit 10
limit 10 will show you the top 10 users and order by desc sort from high to low.
However, the above query will not give you the 0 count as per your question. If you really want the zero count you can try this: (assuming your table name is userlist)
SELECT distinct user_id,
(select
count(*) from `userlist`
where user_id=u.user_id and item_id=324
) as item_count FROM `userlist` u
order by item_count desc
I couldn't create the database in my local, but I think this will do the trick
SELECT user_id, COUNT(item_id) as item_count
FROM TABLE_NAME
WHERE item_id = 324
GROUP BY item_id
ORDER BY item_count;
As we know GROUP BY clause return data by ignoring duplicate data by specifying particular GROUP BY 'user_id' , but i want to do that GROUP BY ignore table row but i want to combine data of that row in array , means i want all data but in filtered row
i want to something like
id | name | user_id
-------------------
1 abc 20
2 trt 19
3 sdf 20
4 khg 22
5 fdf 20
6 lnm 22
id | name | user_id
-------------------
1 abc,sdf,fdf 20
2 trt 19
3 khg,lnm 22
Use GROUP_CONCAT for this. Try this query -
SELECT id, GROUP_CONCAT(name) name, user_id FROM students GROUP BY user_id
The GROUP_CONCAT function concatenates strings from a group into one string with various options.
Update
To implement it in CakePhp -
$driverlocation_data = $this->DriverLocation->find(
'all',
array(
'conditions'=>array('DriverLocation.dispensary_id'=>$dispensary_id),
'fields' => array('id', 'GROUP_CONCAT(name) name', 'user_id')
'group'=>array('DriverLocation.driver_id')
)
);
Please try this query.
Select id, GROUP_CONCAT(name), user_id FROM table_name GROUP BY user_id
I have Used group_concat for name column.
assume I have a table that contain a column named post_id and it has the result like
1
1
1
2
2
3
1
1
I want to loop through all the records and count how many times they exist. What I could thought of is
by while loop
if(result[] = 1){$1++}, but the problem is the value of record is not fixed, it can be 9999..
I'd tried
while ($something= $item->fetch_array()) {
while($test[] = $something['post_id'] > 0){
//logic here
}
}
select post_id, count(*)
from table
group by post_id
This is something you can do in SQL. I believe it would be the following:
SELECT post_id, COUNT(*) FROM tablename GROUP BY post_id;
This will return, for each post_id in the table, that post_id and the count of rows with that post_id.
Have you try this.
Assume:
Table one
**Table one**
Column1
1
1
1
2
2
3
1
1
You can use this query to count it.
SELECT one.column1, COUNT(two.column1) FROM one as one, one as two;
Table:
id value
100 1
101 1
102 1
102 0
103 1
I want the selection to return id's 100,101,103; that is, if the same id has two rows, one with value=1 and a second with value=0, I want to exclude it.
Appreciate any help.
Try this:-
Select id From table Group By id Having count(*) = 1
Could use the having MySQL construct:
SELECT id, count(*) AS tehCount FROM table GROUP BY id HAVING tehCount = 1;
Try this:
SELECT * FROM
table
GROUP BY id
HAVING COUNT(*) = 1
I have an existing table with millions of entries (growing) that consists of:
userid|name|etc...
1 frank ...
1 frank ...
2 joe ...
5 sam ...
1 franky ...
What I need to do is return a table of:
place|name|total
1 franky 3
2 sam 1
3 joe 1
Where total is the SUM(userid = the distinct userid).
Currently I'm doing a query to SELECT DISTINCT userid from table and then foreach returned value in php, I'm doing another query to return the name and sum(userid = userid).
As you can assume, this is very taxing and takes a long time now with all of the values. Is there any way to speed this up by doing 1 query?
i think you need
SELECT #a:=#a+1 AS `place`, name, COUNT(userid) AS `total`
FROM `your_table`, (SELECT #a:= 0) AS a
GROUP BY userid
SELECT userid, COUNT(*)
FROM some_table
GROUP BY userid