Jump to content

Can i do FROM WHERE AND and how?


jonnypixel

Recommended Posts

Hello,

 

I am only new to php/mysql but loving it.

 

Once again i have hit a hurtle when trying to figure out SELECT from 2 tables.

 

I need to compare the listing_ID in my main listingsmanager_listing Table to the listing_ID in my listingsmanager_buyersListings Table.

 

Then i want to show only the listings that the buyer_ID has attached to it from that  listingsmanager_buyersListings Table.

 

The reason i created the third table called buyerslistings is so that a listing can be removed from the buyer with out trashing the listing or the buyer in question.

 

Any help with my code or the logic of how i am doing this would make me very very grateful. :confused:;)

 

So below are my tables in question ( i have reduced the fields to show the example )

 

INSERT INTO `listingsmanager_buyers` (`buyers_ID`, `listingBuyers_name`, `listingBuyers_phone`) VALUES
(52, 'Dave MacAuly', '54 879 079'),


INSERT INTO `listingsmanager_buyersListings` (`id`, `buyers_ID`, `listing_ID`) VALUES
(33, '52', '294'),
(34, '52', '300'),


INSERT INTO `listingsmanager_listing` (`listing_ID`, `listing_Title`) VALUES

(294, 'McDonalds Net Lease Investments'),
(296, 'Kentucky Fried Chicken'),
(298, 'Pizza Hut'),
(300, 'McDonalds Restaurant');

 

And below is my SELECT query

 

$query2 = "SELECT listingsmanager_listing.listing_ID, listingsmanager_buyersListings.listing_ID ".

"FROM listingsmanager_listing, listingsmanager_buyersListings".

"WHERE listingsmanager_listing.listing_ID = listingsmanager_buyersListings.listing_ID".

"AND listingsmanager_buyersListings.buyers_ID =".$buyers_ID;

$result = mysql_query($query2);
while ($row = mysql_fetch_array($result)) {
$listing_ID = $row['listing_ID'];
print ('<fieldset><legend>Listings Attached</legend>');
print('<b>Listing ID:</b> '.$listing_ID.'</fieldset>'); 

}

Link to comment
Share on other sites

Try this out

 

$query2 = "SELECT listingsmanager_listing.listing_ID, listingsmanager_buyersListings.* 
		   FROM listingsmanager_listing
	   RIGHT JOIN listingsmanager_buyersListings
	   ON listingsmanager_listing.listing_ID = listingsmanager_buyersListings.listing_ID
	   WHERE listingsmanager_buyersListings.buyers_ID =".$buyers_ID;

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.