What is the MySQL query to count the total number of rows which contains the same value.
For example:
Consider a table animal which contains all animal names. If there are 5 rows in the table which contains the value name as 'cow' then what would be the query for it?
SELECT animal_name,
COUNT(*) AS animal_count
FROM animal_table
GROUP BY animal_name;
SELECT COUNT(*) AS CowCount
FROM tableName
where animalName = 'cow';
SELECT name, COUNT(*) c FROM animal WHERE name='cow' GROUP BY name
Related
I have the database structure as shown in the below image:
I want to retrieve variant_id that belongs to particular product_id
Example :
lets say 1 and Size = L , Color = Green.
I expect mysql query to return variant_id = 7.
What is the expected query to be used in this scenario?
You can use following query.
I'm assuming your table name is table1
select variant_id from (select * from table1 where
product_id=1 and ((attribute_name='Size' and value='L')
or (attribute_name='Color' and value='Green'))) as temp
group by variant_id having count(*)>1
I'm getting duplicate items of my table doing this:
SELECT * FROM table GROUP BY name HAVING COUNT(*) >1
It returns each duplicate item. But I would like to show:
"repeat X times".
How can I "echo" the "count" in this case?
You should show the count(), instead of *:
select name, count(*)
from table
group by name
having count(*) > 1;
I would like to get one value instead of all values when they have the same name.
within an sql query. Im using fullcalendar. and have two tables one for the events(evenement) and one for the receiver(evenementontvanger).
evenementontvanger:
id idEvent
1 231
2 231
3 231
evenement:
id title
231 hello
I would like to show only one title not 3
my sql query:
"SELECT
*
FROM
`evenement`
JOIN
`evenementontvanger` ON `evenementontvanger`.`idEvent` = `evenement`.`id`
WHERE
`idEvent` = `evenement`.`id`"
You can use distinct to do so as
SELECT distinct `evenementontvanger`.`idEvent`,`evenement`.`title`
FROM
`evenement`
JOIN
`evenementontvanger` ON `evenementontvanger`.`idEvent` = `evenement`.`id`
WHERE
`idEvent` = `evenement`.`id`;
How ever the above will not bother about idWerknemer and if you want to display them as group use Group_concat as
SELECT `evenementontvanger`.`idEvent`,
`evenement`.`title`,
group_concat(`evenementontvanger`.`idWerknemer`) as `idWerknemer`
FROM
`evenement`
JOIN
`evenementontvanger` ON `evenementontvanger`.`idEvent` = `evenement`.`id`
WHERE
`idEvent` = `evenement`.`id`
Group By `evenementontvanger`.`idEvent`
Check the demo here http://sqlfiddle.com/#!2/290b4/13
Use SELECT DISTINCT on your query to eliminate duplicates :
The ALL and DISTINCT options specify whether duplicate rows should be
returned. ALL (the default) specifies that all matching rows should be
returned, including duplicates. DISTINCT specifies removal of
duplicate rows from the result set. It is an error to specify both
options. DISTINCTROW is a synonym for DISTINCT.
From MySQL docs
use either top 1
OR
select distinct
does anyone know how you would count the total number of times a value appears in a single column.
for instance my table looks like:
user_id | liked_id | likes
3 1 1
4 1 1
what i want to do is count the total of the likes column where liked_id matches.
So liked_id 1 has 2 likes?
can someone please show me how i might do this?
function count_likes() {
global $connection;
global $profile_id;
$query = "SELECT likes, count(*) FROM ptb_likes GROUP BY liked_id";
$count_likes_set = mysql_query($query, $connection);
confirm_query($count_likes_set);
return $count_likes_set;
}
Try this:
SELECT liked_id, SUM(likes) FROM ptb_likes GROUP BY liked_id
you should grouped them by liked_id
SELECT liked_id, count(*) totalLikes
FROM ptb_likes
GROUP BY liked_id
if you just want the number of rows with like_id of 1,
SELECT liked_id, COUNT(likes) FROM ptb_likes WHERE liked_id = 1;
If you want the the frequency of each like_id:
SELECT liked_id, COUNT(likes) FROM ptb_likes GROUP BY liked_id;
As a note: don't use SUM as that adds all the values of like_id, and if you have a like_id greater than 1, your count will be wrong. Use count instead.
What i want, to display rows from a table which is selected by a field from other table single value, lets say to display images from a table by last category id.
I have this type of query, but this return me all matching keys rows, if i inset LIMIT 1 then it return one row...
SELECT i.prof_image FROM profile_images i
JOIN images_cat cat ON (cat.cat_id = i.cat_id)
GROUP BY i.prof_image;
//OR LIMIT 1;
Any idea to fix this problem. (i.e. displaying the latest category images)?
This will work for your specific example.. If you need to be more selective, then please post some more details..
SELECT i.prof_image
FROM profile_images i
WHERE cat_id = (select max(cat_id) from images_cat)
SELECT * FROM table_1
LEFT JOIN table_2 ON table_1.id = table_2.id
This query will grab all things in table_2 that have the same id value.
Note that it is a LEFT JOIN - which means that if there are no matching values in table_2, it will still return the values from table_1.
What is your intent with using last()?
Hope this helps.