Jump to content

Problem selecting multiple records from multiple tables


nezbie

Recommended Posts

Hi

I have two database fields looking like this:

[b]distribution_lists[/b] (only relevant fields regarding this question
dlist_id
user_id
email_sent

[b]users[/b] (only relevant fields regarding this question
user_id

$toNewListQ = 'SELECT user_id, (SELECT d.email_sent FROM distribution_lists AS d LEFT JOIN users AS u ON u.user_id = d.user_id WHERE d.dlist_id = "' . $_POST['dlist_id'] . '" LIMIT 0,1) AS email_sent FROM users WHERE user_id IN (' . $givenIDs. ') ORDER BY user_id ASC';

Now, the problem is with this query, that the user who exists on users -table might not exists in distribution_list -table. My goal is to join the users table to the distr.list table and get info about if a certain user has sent email or not, if he/she has the row should return 1. If the user is on the distribution list and he/she has not sent email, it would return 0. If the user does not exist on the distribution list, it should return 0 (for email_sent -field value)

Any ideas ?

Thanx
Link to comment
Share on other sites

SELECT u.user_id, d.email_sent FROM users AS u LEFT JOIN distribution_list AS d ON u.user_id = d.user_id WHERE d.dlist_id = "' . $_POST['dlist_id'] . '" AND u.user_id IN (' . $givenIDs . ') ORDER BY u.user_id ASC

Now that's the query. It works half. It now selects the right value for email sent IF THE USER IS FOUND ON distribution_lists TABLE. There will be a situation that all the IDs given in a comma separated list variable, $givenIDs won't be found on this table.

Considering the query this way might help: 1) select the users info from USERS table. Then join DISTRIBUTION_LISTS to check if users info exists on that table and get only the EMAIL_SENT value true or false.

Half way of winning this :)
Link to comment
Share on other sites

*thinking*

So I have these two tables. I have a form to select users. Those [b]users IDs[/b] are saved in this [b]$givenIDs[/b] variable. (comma separated, in php). My tables are [b]USERS[/b] and [b]DISTRIBUTION_LISTS[/b]

[b]All users[/b] available for my form [b]are already found on USERS table[/b]. Anyhow, on this certain case I have to know if user has [b]already received email[/b]. This info is found on [b]DISTRIBUTION_LISTS[/b] table. Now, the DISTRIBUTION_LIST table might not hold the [b]userID[/b] in progress. If so, [b]email_sent[/b] should be [b]set to 0.[/b]

So the problem with my current query is that if the [b]user_id[/b] exists in [b]USERS table[/b] but NOT in [b]DISTRIBUTION_LISTS[/b] table, mysql for some reason skips this row and [b]returns NO user_id OR NO value for email_sent[/b] - regardless if the user is found on one of the tables.. I have to get some value (null or 0) for [b]email_sent[/b] field for my php script to handle on..
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.