If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > JOINing 2 colums of one table to 1 column of another

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-24-10, 09:11
vivoices vivoices is offline
Registered User
 
Join Date: Jul 2009
Posts: 45
JOINing 2 colums of one table to 1 column of another

I have a table (a) with 3 columns:
txt_id, objName, text

and another Products table (b) including the columns:
short_descr_id, long_descr_id

I JOIN these two tables using:
. . .
ON
b.short_descr_id = a.txt_id
AND
b.long_descr_id = a.txt_id

how do I write the SELECT part to get the objName and text for both short_descr_id and long_descr_id?

Thanks,
David
Reply With Quote
  #2 (permalink)  
Old 02-24-10, 10:49
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 779
SELECT a.objName, a.text

Other than that I think you need to describe your issue better.
Dave
Reply With Quote
  #3 (permalink)  
Old 02-24-10, 11:42
vivoices vivoices is offline
Registered User
 
Join Date: Jul 2009
Posts: 45
Thanks for your reply dav1mo.

The problem is that I have two rows in table "a" that I have to map (JOIN) into each row of table "b".
It might be something like:
SELECT
a.objName, a.text, (for the b.short_descr_id column)
a.objName, a.text (for the b.long_descr_id column)

I assume it is possible but don't know the syntax.

David
Reply With Quote
  #4 (permalink)  
Old 02-24-10, 13:48
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,085
Code:
SELECT a1.objName AS short_name
     , a1.text    AS short_text
     , a2.objName AS long_name
     , a2.text    AS long_text
  FROM table_b
INNER
  JOIN table_a AS a1
    ON a1.txt_id = table_b.short_descr_id
INNER
  JOIN table_a AS a2
    ON a2.txt_id = table_b.long_descr_id
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 02-25-10, 04:46
vivoices vivoices is offline
Registered User
 
Join Date: Jul 2009
Posts: 45
Thanks r937,
here is the syntax that works in my php code:
Code:
"SELECT
	products.product_id,
	products.thumb_img_id,
	products.big_img_id,
	nameID.resource_name,
	descriptionId.resource_name
FROM
	products
	INNER JOIN
		(
			text_resource_names AS nameID,
			text_resource_names AS descriptionId
		)
	ON
		(
			products.name_text_id = nameID.text_id
			AND
			products.description_text_id = descriptionId.text_id
		)
WHERE
	products.page_id = " . $page_id . "
ORDER BY
	products.displayOrder ASC"
I have to code SQL maybe once a month, if at all, so the alias escaped me.

Thanks again,
David
Reply With Quote
  #6 (permalink)  
Old 02-25-10, 05:04
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,085
Quote:
Originally Posted by vivoices View Post
here is the syntax that works
could i ask you to change it slightly please?
Code:
SELECT products.product_id
     , products.thumb_img_id
     , products.big_img_id
     , nameID.resource_name
     , descriptionId.resource_name
  FROM products
INNER 
  JOIN text_resource_names AS nameID
    ON nameID.text_id = products.name_text_id
INNER
  JOIN text_resource_names AS descriptionId
    ON descriptionId.text_id = products.description_text_id 
 WHERE products.page_id = " . $page_id . "
ORDER 
    BY products.displayOrder ASC
note how the INNER JOINs are written

you should stay away from the "comma list" style of joining, you'll just get into trouble
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 02-25-10, 08:12
vivoices vivoices is offline
Registered User
 
Join Date: Jul 2009
Posts: 45
Thanks for your reply Rudy,
may I ask why I could get into trouble using the "comma list" style in the JOINs?
It seems way more readable to me and it is coherent with the SELECT statement.

Is it performance you are concerned about?
Has it something to do with the MySQL engine, I often use a DB Type of InnoDB?

Your positioning of the commas in the SELECT statement makes alot of sense, I will adopt it.

David
Reply With Quote
  #8 (permalink)  
Old 02-25-10, 09:47
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,085
Quote:
Originally Posted by vivoices View Post
may I ask why I could get into trouble using the "comma list" style in the JOINs?
It seems way more readable to me and it is coherent with the SELECT statement.
i say that because i've seen people post problems on forums where the "comma style" join was the cause of the problem -- there are several problems, and i won't go into them, i'll just say that they do come up regularly

as for readable, sorry, i have to disagree -- the explicit JOIN syntax is way more readable, as it isolates each joined table and clearly indicates the join conditions for that table alone

the "leading comma" convention for lists of columns, however, is a separate issue, and is purely a matter of style -- syntactically it is equivalent, but the effect on maintenance (adding or removing columns from the list) is remarkable, at least in my opinion

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 02-25-10, 11:25
vivoices vivoices is offline
Registered User
 
Join Date: Jul 2009
Posts: 45
Thanks Rudy
Quote:
Originally Posted by r937 View Post
the explicit JOIN syntax is way more readable, as it isolates each joined table and clearly indicates the join conditions for that table alone
Agreed, so
Code:
SELECT
		products.product_id
	,	products.thumb_img_id
	,	products.big_img_id
	,	nameID.resource_name
	,	descriptionId.resource_name
FROM
	products
	INNER JOIN
			text_resource_names AS nameID
	ON
			products.name_text_id = nameID.text_id

	INNER JOIN
			text_resource_names AS descriptionId
	ON
			products.description_text_id = descriptionId.text_id
WHERE
	products.page_id = " . $rTxts[0] . "
ORDER BY
	products.displayOrder ASC
is more "fault proof" according to your experience. The next time I have to write complex nested JOINs, I will put the readabillity to test.

David
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On