Jump to content

How do I Use data from 2 tables


jaisol

Recommended Posts

I'm designing a photo gallery, with about 100 pictures per page.

 

I want to store all image file names in a table called images (1 colum - file_name)

I also want to store user details to show if they have deleted an image and not show that image when they redraw the page.

I will call this table User_Deleted (2 colum - user_id, file_name)

I'm thinking of doing something like

 

$user_id = the current user

Select * from user_deleted where user_id = $user_id

retrive the file names and store into an array

 

run a loop to add " AND file_name != next file name in the array

and then parse it onto the end of a Select command

 

Select * from images WHERE file_name != 1st deleted file AND  file_name != 2nd deleted file AND .. however many files have been marked   LIMIT 100

I'm sure there must be an easier way of doing it, but I'm not smart enough to see it LOL

 

I think something like

 

Select * from images but not if they are in Select * from user_deleted where user_id = $user_id

would be a better way but I can't figure out how you write that sort of statement.

 

Link to comment
Share on other sites

Hi

 

You can do it in a single SELECT statement (something like as follows).

 

SELECT *
FROM images
LEFT OUTER JOIN user_deleted
ON images.file_name = user_deleted.file_name
WHERE user_deleted.file_name IS NULL

 

Note, it would probably be better to have 2 columns in the images table, one being an auto incremented id and the other the file name. And refer to the id field in the user_deleted table. A join on simple numeric fields like this should be quite a bit more efficient (both in speed and also in how much storage is used once you factor in all the possible rows on user_deleted).

 

All the best

 

Keith

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.