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;
Related
I have a table with the following columns: id, firstname, lastname, category.
And let's say i have data with categories like: action, comedy, drama, horror.
My query is
SELECT * FROM tablename WHERE firstname LIKE '%John%'
The query will bring all the records that contain 'John' in firstname, and let's assume that the records have different values for categoty: action and comedy only.
How to find out the list with all the values available in category column for this specific search.
Thanks!
If you want a distinct list of categories, where firstname is like John:
SELECT DISTINCT category FROM tablename WHERE firstname LIKE '%John%'
SELECT category FROM tablename WHERE firstname LIKE '%John%'
Simple as it looks
try this:
SELECT *
FROM tablename
WHERE firstname LIKE '%John%'
and category='category_name'
If you want the category list with a count on the number of occurrencies:
SELECT DISTINCT CATEGORY, COUNT(*) AS Total FROM tablename WHERE firstname LIKE '%John%' GROUP BY CATEGORY ORDER BY CATEGORY
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`
I have a table with a userID field and an itemID field. I would like to select all of those users that have two or more instances where itemID is the same (that is, if for example there are 3 records where userID = 1 and itemID = 7 then I would like those results, but not if there's just one instance). I need to get all users (not just results for a certain userID).
Can anybody suggest how I could do this?
Thanks.
You can do this using aggregation and a having clause. If you just want the users:
select distinct userID
from t
group by userId, itemID
having count(*) >= 2;
This is an interesting query because it is one of the very rare situations where group by and select distinct are used together. If you wanted the userId/itemId pairs, then you would use select userId, itemId, without the distinct.
You just need to use group by and having. The having clause is like where except that it also works on aggregations. So it's something like select userID, itemID, count(*) from mytable group by userID, itemID having count(*) > 1.
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
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.