tags:

views:

231

answers:

5

Using:

    SELECT * 
      FROM cart 
INNER JOIN dkb ON cart.id = dkb.id 
     WHERE cart.cookieId = '" . GetCartId() . "'"

Besides the dkb table, I want to INNER JOIN the cdkb table using ON cart.id = cdkb.id.

How would that be displayed when cdkb added to the existing query?

+6  A: 

Something like this

SELECT *
FROM Table1 t1 INNER JOIN
Table2 t2 ON t1.ID = t2.ID INNER JOIN
Table3 t3 ON t1.ID = t3.ID INNER JOIN
....
TableN tn ON t1.ID = tn.ID

So it would look like

SELECT  * 
FROM    cart INNER JOIN 
        dkb ON cart.id = dkb.id INNER JOIN 
        cdkb ON cart.id = cdkb.id
WHERE cart.cookieId = '" . GetCartId() . "'
astander
Ok I have an url that contain the cdkb.id or dkb.id and then that id will activate cart.id depending on the id of one of the tables? What about if the id coming in the url is found in both tables? how will the query now which table to activate?
jona
+1  A: 

You can use as many JOIN as you want -- just add them to the from/join section of the query :

select *
from cart
    inner join dkb on cart.id = dkb.id
    inner joion cdkb on cart.id = cdkb.id
WHERE cart.cookieId = '...'

And, for the details, you can take a look at this section of the manual : 12.2.8.1. JOIN Syntax.

Pascal MARTIN
thank you very much
jona
Some may argue that when the number of joins exceeds 7 then there could be possible normalization issue with the DB.
Saif Khan
A: 

Exactly as you have described it:

SELECT 
   * 
FROM 
    cart 

    JOIN dkb 
       ON cart.id = dkb.id 

    JOIN cdkb 
       on cart.id = cdkb.id
WHERE 
   cart.cookieId = GetCartId()

regarding OUTER JOINs, here is a link discussing them:

if you do:

SELECT
   cart.id    cart_id,
   dkb.id     dkb_id,
   cdkb.id    cdkb_id,
   cart.*,
   dkb.*,
   cdkb.*
FROM
    cart

    LEFT OUTER JOIN dkb
       ON cart.id = dkb.id

    LEFT OUTER JOIN cdkb
       on cart.id = cdkb.id
WHERE 
    cart.cookieId = GetCartId()

That means you will always get every matching record from cart, information from the dkb and cdkb tables will only appear if records with that ID are available in them, otherwise their fields will be NULL. I used a LEFT OUTER JOIN because I want all the records from the cart table, which is on the LEFT side of the ON condition.

A FULL OUTER JOIN would mean that either the cart.* or the dkb or cdkb fields could be NULL.

It definitely takes awhile to get a feel for the OUTER JOIN syntax, as you can tell since I still got it wrong explaining it (the first time). :-)

Ron Savage
I was not sure on that but thanks for your extended hand.
jona
Simply formatting the query the way I have, makes your intent and the results much easier to understand. :-)
Ron Savage
the url will have an id and then that ide will activate cart.id depending on the id of one of the tables? What about if the id coming in the url is found in both tables? how will the query now which table to activate?
jona
The id has to be in both tables or this query won't return any results. If the ID might not be in one of the tables, change the JOIN to an OUTER JOIN ... that will show results from one table and NULLs for the other table fields.
Ron Savage
I get it now, The OUTER JOIN seem to be some kind of a solution but I am not sure, I am looking for a query that identify which table is sending information. It's only going to be one table sending one information at the time. It can be either or and the query needs to be ready to display data according to the table.id. now there is going to be one table sending it's id either dkb.id or cdkb.id. so I am afraid that the query migth get confused? To check what table, to display. after reading some of the posts above bu that's why aliasing fields willcome in effect.
jona
That;s a good idea that always display the cart fields, because that's the intention here to display the cart fields and it will display by either being activated from the dkb.id table field that comes from page1.php and cdkb.id field coming from page2.php. I have noticed that you have used dkb.id in both JOINs for cdkb table you meant cdkb.id right? i just want to clarify that to be clear of what i understand is correct. Thank you very much for the illustration. i have another question why in the SELECT clause you use cart.id and cart.* as well? with the cart.* was not enough?
jona
What was the reason for you to use cart.id and cart.* thank you.
jona
I put the .Id fields from all three tables first, with column alias names to identify which was which - to make it easy to see which tables had data and which didn't. Then the cart.* was just to be able to see all the fields in the table.
Ron Savage
I meant in the first and second RIGHT OUTER JOIN clauses which you have compare cart.id with dkb.id in both clauses where I believe it should be as you put in the SELECT clause. I believe thats a typo. there. thank you for the link where JOINs are explained if you visit the link you will notice that in the definition for RIGHT OUTER JOIN will display all the fields on the Right of the ON clause and you have said that the desired affect is to display all fields of cart table and then display the right side of the ON if the condition is met. Soo in that case would LEFT OUTER JOIN work?
jona
this would work for thiscase where it will always display the fields of cart tables?
jona
I have a question the alias names are the one to be used in the php script? right?I also saw you didn't use the alias in the RIGTH OUTER JOIN clause. Well I can imagine those are the fields directly from the table some you can't use the alias but then alias are the one to be used in the php script to display the data in the browser.
jona
You are right, I had a typo in there and got the LEFT and RIGHT outer joins backwards. :-) Should be fixed now. In your result data, those first 3 columns will be named cart_id, dkb_id, cdkb_id if you name them like that.
Ron Savage
cart_id, dkb_id, cdkb_id in other workds those fields are going to be the one to use in the php script instead of cart.id, dkb.id, or cdkb.id, right?Thank you for the clarificatio.
jona
I have a question someone in the forum told me that if I have to acept the answers I don't know what he really meant but I would like to know what is that and how can I acept the answers
jona
Guys how can i accept the answer? all of them are 0% percent
jona
You can accept any answer you want, doesn't matter on the votes - just pick the one that helped you solve your problem the most.
Ron Savage
+1  A: 

Best practice would be to explicitly specify field names per table, and use an ALIAS on each table, e.g.

SELECT
 c.field1 AS c_field1
, c.field2 AS c_field2
, c.field3 AS c_field3
, d.field1 AS d_field1
, c2.field1 AS c2_field1
FROM cart AS c
INNER JOIN dkb AS d
  ON (c.id = d.id)
INNER JOIN ckdb AS c2
  ON (c2.id = c.id)
WHERE c.cookieID = '{VALUE}'

Of course this glib example doesn't let the merits of an alias shine; generally if you have a table name like CustomersBankAccountData, then an alias is ideal. ;)

EDITED: updated to show aliased field names, which makes perhaps more sense in this case than even aliasing the tables.

bdl
I think this would be good, Since dkb and ckdb has the same field names which can conflict when assigning and differentiating them in the script.
jona
@jona> check my edited post for aliased field names.
bdl
the indexes below that query in the php will activate depending on the table that has been requested?
jona
What I meant is that I have an url that contain the cdkb.id or dkb.id and then that id will activate cart.id depending on the id of one of the tables? What about if the id coming in the url is found in both tables? how will the query now which table to activate?
jona
You'll need to use some logic in your script to dictate the context of the id value and use that context in your WHERE clause. In other words, if you have a url like `target.php?cdkb=200`, then your script must look at the query string, figure out that the id value relates to the 'cdkb' table, and use this table and value in the WHERE clause, dynamically.
bdl
A: 

you may find this article helpful:

http://www.devshed.com/c/a/MySQL/Understanding-SQL-Joins/

is says MySQL, but it's pretty generic in the explanation of JOINS (INNER, LEFT and RIGHT)

Leslie