views:

167

answers:

1
+1  A: 

It looks like if you are passing in the user and catalogue id's then the supplier doesn't matter.

If you required the supplier information in the result, that would be a different matter.

It feels like you shouldn't be involving the user in this query at all as you seem to be looking for the images in a catalogue that are owned by a particular supplier.

If that is the case, then I would drop the requirement for the user id in the query and use the supplier id instead.

I am assuming that the user would have done the following to get to the point where they would be initiating this query:

  • login - obviously :)
  • click on 'list suppliers'
  • click on a supplier
  • click on a catalog

Either way you are going to have to do a lot of INNER JOIN's. For instance the query to retrieve the list of suppliers for a given user would be something like

SELECT 
    s.supplier_id, 
    s.Supplier_name
FROM
    supplier s
INNER JOIN
    user u
INNER JOIN
    user2supplier u2s
ON
    u.user_id = u2s.user_id
ON
    u2s.supplier_id = s.supplier_id
WHERE
    u.user_id = 3 -- for example...

(now, I haven't tested the SQL, but I think that is right...)

Let me know if I'm on the right track - if I have helped, I'd be happy to help some more if I can

mlennox
Definitely on right track I guess. But the approach is wrong for me :) I want the user to click on a catalogue and then get all images listed that they have access to, depending on what suppliers they have access to. They never click on a list of suppliers. I have managed to list only the catalogues each users have access to, depending on users supplier access. So now I need to list the correct images depending on catalogue and supplier.
jamietelin
Even if your query for listing suppliers isn't really what i was looking for, i feel like i should correct it :) This works better: ON u.user_id = u2s.user_id AND u2s.supplier_id = s.supplier_id; My Question is still open though and I am very thankful for any help I can get!
jamietelin
:) seems a bit better - it was late last night and I wrote it without too much thought - I don't have the time to think about it just yet, but I will over the next 24 hours and I intend to get back to you now that I have a clearer idea of what you are aiming for
mlennox
Thank you mlennox for taking the time to help me! I appreciate it. :)
jamietelin
Could you provide your schema creation script and some example data Jamie - it would make this a LOT easier...
mlennox
You'll find schema creation script and some example data above now as I've updated my question.
jamietelin
Makes any sense? Think you can help me?
jamietelin
Thanks - I'll have a look at that, but just one comment from looking at the PHP (?) code above - sanitise your inputs! You seem to have a serious SQL injection vector there - can PHP do parameterised queries? http://xkcd.com/327/It's late here so I don't think I'll get to work on the problem tonight - you'll hear from me though!
mlennox
The creation script and data i made just for you too see (or try out yourself), thats not used on actual site. I check all data before its saved into mysql via PHP. But thanks for the concern.
jamietelin