Hopefully someone can help. I’m busy building a e-commerce website in dreamweaver. The website will have have music cd’s, dvd’s, movies, and games for the moment. I have a database consisting of most of the above mention.
Now i have built all of it myself and am slowly learning as I go(I know a bit of code, SQL, PHP, etc.)
I have created a search bar which searches the database for whatever keyword was inserted and returns results on a new page. I thought i had created the ultimate piece of sql but one day i did a search of an album that had a track listing and it came up with repeats of this specific album as many times as there were songs on the album eg 10 songs=10 repeats of the album which to a user is not useful.
This is my code:
SELECT
Department.Dept, Artist.Artist, Catalogue.Title, Tracklisting.TrackTitle,
Catalogue.ImagePath, Catalogue.Sell, Catalogue.ID
FROM Department
INNER JOIN ((Artist INNER JOIN Catalogue ON Artist.ID =
Catalogue.Artist_ID) LEFT JOIN Tracklisting ON Catalogue.ID = Tracklisting.CatID) ON
Department.ID = Catalogue.Department_ID
WHERE Department.Dept Like '$select' AND Artist.Artist='$search' OR Department.Dept
Like '$select' AND Artist.Artist Like '%$search' OR Department.Dept Like '$select' AND
Artist.Artist Like '$search%' OR Department.Dept Like '$select' AND Artist.Artist
Like '%$search%' OR Department.Dept Like '$select' AND Catalogue.Title='$search' OR
Department.Dept Like '$select' AND Catalogue.Title like '%$search' OR Department.Dept
Like '$select' AND Catalogue.Title like'$search%' OR Department.Dept Like '$select'
AND Catalogue.Title like'%$search%' OR Department.Dept Like '$select' AND
Tracklisting.TrackTitle='$search' OR Department.Dept Like '$select' AND
Tracklisting.TrackTitle like'%$search' OR Department.Dept Like '$select' AND
Tracklisting.TrackTitle like'$search%' OR Department.Dept Like '$select' AND
Tracklisting.TrackTitle like'%$search%'
This is an ODBC connection and all of these $search and $select are my php getting the info from the search bar and the department selection eg. music or games
There are 4 tables in database where i am pulling the info from. Artist – Catalogue – Tracklisting and Department. All 4 are linked and i have made the link between tracklisting and catalogue so if there is no tracklisting it still pulls the album info up
Ovblisly each album has multiple tracks so each track of the same album has the same id linked to the album that has a unique id and then linked to artist table where each artist has a unique id.
The query then gets displayed into a table in HTML in PHP so to get displayed over and over depending on if there are albums to get displayed repeatly but diffrent albums not the same as it does now with the traclisting.
I have searched high and low for answers and this morning i thought i had found it…GROUP BY
I have been unable to get group by to work and keep receiveing this error You tried to execute a query that does not include the specified expression ‘Dept’ as part of an aggregate function.
Is there a way to tell php to only display one item if it starts to repeat or does it need to go into sql with GROUP BY which i was using incorrectly.
I have found that if i remove the track listing from the sql query it is fixed but then no one will be ale to search by song. Maybe there is a way to create 2 different queries??
Any help will be much appreciated.
EDIT!
This was the final code that Larry answered for me which i am very gratefull for. Remous code may also work but i have to make it work
"SELECT Department.Dept, Artist.Artist, Catalogue.Title, Catalogue.ImagePath,
Catalogue.Sell, Catalogue.ID
FROM Department INNER JOIN (Artist INNER JOIN Catalogue ON Artist.ID =
Catalogue.Artist_ID) ON Department.ID = Catalogue.Department_ID
WHERE Department.Dept Like '$select' AND (Artist.Artist Like '%$search%' OR Catalogue.Title like'%$search%' OR Catalogue.ID IN (SELECT CatID
FROM TrackListing
WHERE TrackTitle like'%$search%'))";
Many thanks to everyone that helped!!!
First off, if you want to use GROUP BY then you must include every column you list in the SELECT in the GROUP BY:
(you don’t list columns that are in an aggregate function like SUM or COUNT, but you’re not using any of those columns).
But that isn’t going to solve your problem. I gather that even if you find track listings that match the search (which, please, must be escaped or parameterized to prevent SQL injection) then each track will be returned and GROUP BY won’t remove the records because the track name will make them unique.
Instead, you need to remove tracklisting from the list of JOINed tables. Then you must remove references to the tracklisting columns from the SELECT and WHERE clauses.
Finally, if you want to be able to search for albums that have tracks that contain the specified text, add an IN () subquery to your search.
You’ll wind up with something like this:
Note that I remove a lot of apparent redundancy from your WHERE clause. If you’re searching, for instance, on
Artist LIKE '%$search%'then you do not also need to do the other searches on Artist, you only need the broadest search. And you were repeating the Dept search for each other search (I assume you generated the query from Access’s query builder) but with proper parenthesization that’s not necessary.