SQL DISTINCT plus count - php

I currently have a table with album name, artist, and title in it (for Mp3s). On my webpage on the left I currently display all album names using:
SELEC DISTINCT `album` FROM `mp3`
But I would like to display the amount of each album next to it, for instance:
A Very Good Album (3)
Indicating that there are 3 entires in that record. Can I do this with SQL? Select Distinct, but also find out how many are with that album?

Try this:
SELECT `album`, COUNT(*) FROM `mp3` GROUP BY `album`

Rather than DISTINCT, this is really a basic COUNT() aggregate grouped by album.
SELECT
album,
COUNT(*) AS num_records
FROM
mp3
GROUP BY album
If you want it in the parentheses inside the SQL query, use CONCAT(). This is probably best left to your application for display though.
SELECT
/* The album title, and count in () as one column as a single string */
/* like "A Very Good Album (3)" */
CONCAT(album, ' (', COUNT(*), ')') AS album
FROM
mp3
GROUP BY album

You want a group by:
select album, count(*)
from mp3
group by album

use the group by and concat the string like this
SELECT
CONCAT(`album`, ' (', COUNT(*), ')') as album
FROM `mp3`
GROUP BY `album`

Take a look at Count() and Group By

SELECT DISTINCT CONCAT(album, ' (', COUNT(DISTINCT album), ')') AS album
FROM mp3
GROUP BY album

Related

SELECT DISTINCT on one column, with multiple columns in mysql

I have to display result with select multiple column but distinct on only one column i have also used following queries and also refereed below link.
Query One.
select category , offer_id, store_image from `tbl_coupan_offer` where
`offer_id` in (Select max(`offer_id`) FROM `tbl_coupan_offer` group by `category`)
Above queries return all record with including duplicate
I want only display distinct category not repeated
Following is image
On image you can see Travel and Accessorises repeated
Second query
SELECT DISTINCT `category`,`offer_id`,`store_image` FROM `tbl_coupan_offer`
I also refereed this link Select all columns from rows distinct on one column
Remove offer_id from the select:
SELECT DISTINCT category
FROM tbl_coupon_offer;
If you want one offer_id, then use GROUP BY:
SELECT category, MAX(offer_id)
FROM tbl_coupon_offer
GROUP BY category;
Like Gordon said but as a subquery to get the appropriate image.
SELECT DISTINCT q.category, ch_offer_id, store_img
FROM
(
SELECT category, MAX(offer_id) ch_offer_id
FROM tbl_coupan_offer
GROUP BY category
) q
JOIN tbl_coupan_offer
ON q.ch_offer_id=tbl_coupan_offer.offer_id
SELECT category , MAX(`offer_id`) , store_image
FROM `tbl_coupan_offer`
GROUP BY `category`
If multiple data coming then just add DISTINCT after SELECT
Check your database engine type. It is "InnoDB" or not. If your database and table both existed and if you find this error #1146 Table xxx doesn't exist. Then read more about database engine.
If all is right, then query of Gordon is right.
SELECT `category`, `offer_id`, `store_image`
FROM `tbl_coupon_offer`
GROUP BY `category`

SQL SELECT DISTINCT confusion

I know I can select distinct like this:
SELECT DISTINCT id, title, photo FROM myTable;
But when I want to SELECT id, title but with distinct title, what should I do?
I mean I want to select rows and avoid duplicate titles. Because maybe there be duplicate photos, but it's not important, I need only distinct titles. How then should I select photo and title fields and at the same time set title to be unique and distinct?
SELECT id, title, photo FROM myTable GROUP BY title;
simple select query with group by will work here.
Depends on your data, but basically you are looking to force photo to return a single value, along with your distinct title. Something like this:
SELECT title, MAX(photo)
FROM myTable
GROUP BY title
would do that, for example.
Distinct Photo Menas all photo without no repetition
SELECT id, photo FROM myTable GROUP BY title;

Get results from multiple rows with same names

I have some tables with various content. What I want to accomplish is to display the lets say 20 latest entries from those tables in a div.
Here is my first table - audio1
userID folder title date
the two other folders look exactly the same
audio2
userID folder title date
audio3
userID folder title date
How can I get the data from all the tables at the same time and echo them one by one to a div ordered by date with PHP?
SELECT userID, folder, title, date
FROM audio1
UNION ALL
SELECT userID, folder, title, date
FROM audio2
UNION ALL
SELECT userID, folder, title, date
FROM audio3
ORDER BY date DESC LIMIT 20;
Your SQL query will be a 3 part UNION. A UNION query concatenates results from multiple tables with (usually) similar structures, when a JOIN relationship is not needed but rather you just need to return rows from multiple tables.
$sql = "SELECT userID, folder, title, date FROM audio1
UNION ALL
SELECT userID, folder, title, date FROM audio2
UNION ALL
SELECT userID, folder, title, date FROM audio3
LIMIT 20;";
$result = mysql_query($sql);
if ($result) {
// Fetch results and echo them into a list.
}
You will need a column to ORDER BY. This is likely to be date, but you may have other plans. Add an ORDER BY clause before the LIMIT 20.
it seems that your database setup is wrong and you have to have only one table called 'audio' with a field represents the number you are currently using in the table name.
You may try the union, or you can try a join.
SELECT
COALESCE(t1.userID, t2.userID, t3.userID) as userID
, t1.folder, t1.title, t1.date
, t2.folder, t2.title, t2.date
, t3.folder, t3.title, t3.date
FROM table1 t1
FULL OUTER JOIN table2 t2 ON (t1.userID = t2.userID)
FULL OUTER JOIN table3 t3 ON (t1.userID = t3.userID)
It's a very different beast, so I'd thought I'd give you the option.

selecting id and count(id) from table in mysql

I am trying to make a photo album system in php and mysql.
I have this code for list all photo albums:
SELECT albums.*, count(pictures.id) AS countpic
FROM albums
LEFT JOIN pictures ON (albums.id=pictures.album_id)
GROUP BY albums.id
ORDER BY albums.date DESC");
I want to list title of photo albums with photo thumbnails (thumb_id si written in table pictures) and also with number of all pictures assigned to each albums.
Dont you know how to select thumb_id and also number of all pictures in album by using one select?
Thanks
Does adding group_concat(pictures.thumb_id) to the select list do what you need?
This would work if for each album there is a single image with is_thumb=1.
SELECT
albums.*,
count(pictures.id) AS countpic,
group_concat(pictures.thumb_id) ids,
group_concat(case when picture.is_thumb then pictures.thumb_id else '' end separator '') thumb
FROM albums
LEFT JOIN pictures ON (albums.id=pictures.album_id)
GROUP BY albums.id
ORDER BY albums.date DESC");

Search in more than one table - give result as one column

Hey stackoverflow - This is my first question here.
I have 2 tables in my mySQLdb.
[tab_artist]
|id|artist|count
[tab_songtitle]
|id|songtitle|artistId
Im trying to select from both tab_artist.artist and tab_songtitle.songtitle as suggest where searchclause = m
I have tried this
SELECT artist, songtitle AS suggest
FROM tab_artist, tab_songtitle
WHERE artist LIKE('m%') OR songtitle LIKE('m%')
But this gives me 2 columns, artist and suggest
if the search is met i need artist to give me e.g. metallica.. but only once - but in songtitles i need all titles starting with met.
Hope this makes sence to the right expert :)
A union should do it:
select artist AS suggest from tab_artist WHERE artist LIKE 'm%'
union all
select songtitle AS suggest from tab_songtitle WHERE songtitle LIKE 'm%'
For this case, I would use union all so you won't get duplicates removed, but you may want this if, for example, Metallica has a self-titled album and you only want the word to appear once. In that case, union would be more appropriate.
You need a join:
Select artist, songtitle from tab_artist inner join tab_songtitle on tab_songtitle.artistID = tab_artist.ID where artist LIKe ('m%') OR songtitle like ('m%')
you want to use SQL UNION
select id, artist, count FROM table1
UNION
select id, title, artistid from table2
It basically concats the two result sets vertically. There are some restrictions and modifications, but generally thats all you need to do.
Use a UNION ALL to concatenate the results from the artist and song tables.
SELECT 'Artist' AS type, id, artist AS suggest
FROM tab_artist
WHERE artist LIKE 'm%'
UNION ALL
SELECT 'Song', id, songtitle
FROM tab_songtitle
WHERE songtitle LIKE 'm%'
Don't use UNION as some are suggesting as that will do a completely unnecessary (and expensive) DISTINCT on the query results.

Categories