user2025934
user2025934

Reputation: 129

3 table SQL Query

I dont have huge experience with SQL so I'm trying to formulate a query with the following tables:

   TABLE `customerdeal` (
   `id` ,
   `customerid` 
   `dealid` 
   PRIMARY KEY (`id`),
   KEY `dealid` (`dealid`),
   KEY `customerid` (`customerid`),


  TABLE `deal` (
  `dealid`,
  `dname` 
  `description` 
  `restaurantid` 
   PRIMARY KEY (`dealid`),
   KEY `restaurantid` (`restaurantid`)


  TABLE `restaurant` (
  `restaurantid` 
  `name` 
 `username` 
 `password` 
  PRIMARY KEY (`restaurantid`)

Basically the Customerdeal table holds 2 id values - a customerid and a dealid. I have already successfully created an SQL code which will render the customerid of the logged in user which is being stored in a $_customerid value.

In plain english what I'm trying to do now create an SQL statement which will:

I've been back and forth trying different ways but none of them work! If anyone can help me out I'd appreciate it! Thanks!

Upvotes: 0

Views: 104

Answers (2)

Joe
Joe

Reputation: 1388

I'm surprised that you haven't declared any types for any of the columns.

I am not sure I understand what it is you're trying to do with your query though. Could you clarify? You could do something like this:

    select d.dname as dealname, d.description as dealinfo, 
    r.description as restaurantinfo, r.name as restaurant name 
    from deal d, restaurant r, customer c 
    where r.restaurantid = d.restaurantid, d.dealid = c.dealid,
    c.customerid = 'whatever you want'

Upvotes: 1

Becuzz
Becuzz

Reputation: 6866

If I understand your problem correctly, the following should work.

SELECT d.dname AS DealName, d.description AS DealDescription, r.name AS RestaurantName
FROM customerdeal c 
INNER JOIN deal d on d.dealid = c.dealid
INNER JOIN restaurant r on r.restaurantid = d.restaurantid
WHERE c.customerid = @customerid

You need to substitute @customerid for your actual customer id.

Upvotes: 0

Related Questions