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.
Related
I got the following structure:
admin_id || country_id
---------------------------------
1 2
5 1
1 2
2 3
5 62
1 1
3 62
How to fetch all values by taking the $_SESSION['admin']['id'], finding the country_id of that admin and getting all other admin_id and country_id that are the same of the session admin?
So, lets say the currently logged in admin has id = 5 , that means the admin_id: 5 has two country_id: 1 and 62. I want to take all rows that have country_id: 1 and 62.
It should return this:
admin_id || country_id
------------------------
5 1
5 62
1 1
3 62
How can I do this in one sql query?
You can use a where clause for filtering on the admin_id or the country_id:
select t.*
from t
where t.admin_id = 5 or
t.country_id in (select t2.country_id from t t2 where t2.admin_id = 5);
SELECT t1.*
FROM table t1
JOIN table t2 USING (country_id)
WHERE t2.admin_id = 5
fiddle
I am wanting to convert data that is queried from the database to another string. The best way I can explain what I'm trying to do is below:
I have a table called "users". Inside that table I have a column called "rank". Rank is a two-digit integer ranging from 1 to 21.
I want to convert that "1" or whichever number into more understandable text in my php file. So if your rank is set to "1", the output is "User" or what-have-you.
How do I go about doing this? I'm not sure what this process is called. I'm very new to php.
Thank you!
Create a rank table having 2 columns, id and rank:
id | rank
-------------
1 | user
2 | admin
3 | goof
Then JOIN the users table with the rank table:
SELECT u.name, r.rank
FROM users u
LEFT JOIN rank r
ON u.rank = r.id
You can store the ranks title in another table like mentioned by Jay Blanchard or you can define an array with the rank titles like this
$ranks = array(
1 => 'User',
2 => 'Administrator',
3 => 'Moderator'
);
and can echo like this
echo $ranks['rank_id_fetched_using_sql'];
rank_id_fetched_using_sql is supposed to be 1 to 21
this is sample product properties table
product_id property_id property_value
5 1 white
5 2 50
5 3 50
5 4 55
5 5 mm
6 8 cm
i want filter my products dynamically. for example:
select property_id 1 and property_value white
AND
select property_id 2 and property_value 50
AND
select property_id 4 and property_value 55
AND
etc ...
i can make dynamic query from a basic query. when i use all conditions together, no record matched because all conditions operator is AND. what is the best query?
thanks for your answers.
It sounds like you're using the wrong keyword. Use OR instead of AND between the sets of criteria:
SELECT DISTINCT product_id
FROM product_properties
WHERE
(property_id = 1 and property_value = 'white')
OR
(property_id = 2 and property_value = '50')
OR
(property_id = 4 and property_value = '55')
OR
etc ...
You can use DISTINCT if you just want one of each product_id that matches the various criteria, or leave it off to get one for each row that matches.
Heading ##The basic query is:
SELECT
a.`*`,b.`*`
FROM
properties AS a
JOIN
properties AS b
ON
a.`product_id` = b.`product_id`
WHERE
a.`property_id` = '1' AND a.`property_value` = 'white'
AND
b.`property_id` = '2' AND b.`property_value` = '50'
Thanks to #mikhail-vladimirov https://stackoverflow.com/a/15208055/5129662
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;
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);