Jump to content

Getting one record from one table and multiple records from another table


Recommended Posts

Howdy,

 

OK, I have been struggling with this problem all evening yesterday and this morning. Please help!

 

I have a table subscribers that holds the emails of subscribed users. Then I have the table posts that holds the blog posts.

I want to make it so that with one query I pull out all the emails from the subscribers table and only ONE record from the posts table namely the post_id and the post title so that I can that way construct a link to where the user can click on their email that will take them to the latest blog post. I can make it with two queries, but I really want to do it with one. I tried everything, sub-queries, joins, crosses, prayers with no good. 

 

I wrote this:

SELECT DISTINCT (SELECT DISTINCT MAX(post_id) FROM posts ) AS last_post ,  subscribers_email
FROM subscribers 
 

But it always pulls out duplicate post_id rows like this:

post_id subscriber_email
66 example1@gmail.com
66 example2@gmail.com

Here is my PHP code:


		function send_mass_email($db){
			$sql = 'SELECT subscribers_email, post_id, title
					FROM subscribers, posts
					WHERE CONDITION';
			$query = $db->prepare($sql);
			$query->execute();
				$suject = 'New blog post in www.dyulgerova.info';
			$message = '
			New blog post have been posted in www.dyulgerova.info/blog		Link - *LINK HERE*	';
			while($row = $query->fetch(PDO::FETCH_NUM)){
				mail($row[0], $subject, $message);
			}
		}

Thank you very much!

Edited by Stefany93

So you have two tables which are unrelated (so a join is out) and different structures (so a union is out).

 

I would stick with two queries or prayer.

 

Damn it... Spend so much time on an un-solvable issue. So smart of me... :(

 

Thanks man.

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.