Jump to content

Archived

This topic is now archived and is closed to further replies.

DylanBlitz

Query question

Recommended Posts

okay, here is my problem. I\'m building a custom shopping cart for online CD sales. These CD\'s are full CD\'s, split CD\'s and compilation CD\'s. Getting the full and comp CD\'s to list is easy but the split ones are giving me trouble. Maybe it\'s my db layout?

 

Here is how my tables are layed out.

 

Albums

-----

album_id

album_name

album_band1 (contains a band_id from the bands table)

album_band2 (contains a band_id from the bands table)

album_type

 

 

 

Bands

----

band_id

band_name

 

 

Type

----

type_id

type_name

 

 

now my problem is when I do a select on all bands that start with the letters A-D I only get full CD\'s or split CD\'s where the first band listed starts with A-D

 

[php:1:4e968af46f]

$query = \"SELECT album.album_id, album.album_name, album.album_qty, album_price, album_euprice, bands.band_name FROM album INNER JOIN bands ON album.album_band1 = bands.band_id WHERE ((bands.band_name LIKE \'A%\') || (bands.band_name LIKE \'B%\') || (bands.band_name LIKE \'C%\') || (bands.band_name LIKE \'D%\')) ORDER BY bands.band_name LIMIT $from, $max_results\";

[/php:1:4e968af46f]

 

Is there a way I can join a table twice? I don\'t know any other way to check the names for the album_band2 id

Share this post


Link to post
Share on other sites

$query = \"SELECT album.album_id, album.album_name, album.album_qty, album_price, album_euprice, A.band_name, B.band_name

FROM (album INNER JOIN bands A ON album.album_band1 = A.band_id )

INNER JOIN bands B ON album.album_band2 = B.band_id

WHERE (LEFT (A.band_name,1) IN (\'A\',\'B\',\'C\',\'D\') ) ||

(LEFT (B.band_name,1) IN (\'A\',\'B\',\'C\',\'D\') )

ORDER BY A.band_name LIMIT $from, $max_results\";

 

Of course it might be easier to design it with the bands in a child table rather than having the 2 repeated fields.

Share this post


Link to post
Share on other sites

thanks for the reply barand :)

the query kind of works heh. It only will work if there is a band id in both fields, wont work if there is just one band. What did you mean by designing it with the bands in a child table?

Share this post


Link to post
Share on other sites

Like this :

 

Albums

-----

album_id

album_name

album_type

 

Album_bands

---------

album_id

band_no

album_band (contains a band_id from the bands table)

 

Bands

----

band_id

band_name

 

 

Type

----

type_id

type_name

Share this post


Link to post
Share on other sites

IF you change the INNER JOINS to LEFT JOINS, that should improve things if only one band.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.