473,387 Members | 1,664 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

MYSQL: Retrieve one row with data from JOINED tables with multiple matching rows

56
Hi guys,

I'm not sure this is possible in one SQL query:
I have a table called TB_EMAIL and another called TB_OTHER_BLOB.
TB_EMAIL contains details like: to, from, subject, body and since I will be reusing headers and footers I aslo store header and footer reference to TB_OTHER_BLOB in TB_EMAIL.

What I need is a row (given its id) with all columns (except the references to TB_OTHER_BLOB) from TB_EMAIL and two extra columns one being email_header and the other email_footer which are in two different rows in TB_OTHER_BLOB

What I came up with is:

Expand|Select|Wrap|Line Numbers
  1. SELECT     tb_email.email_to_email,
  2.     tb_email.email_from_email, 
  3.     tb_email.email_from_display,
  4.     tb_email.email_subject,
  5.     tb_email.email_body,
  6.     tb_other_blob.blob_text     
  7.  
  8. FROM    tb_email 
  9. INNER JOIN tb_other_blob ON
  10.       tb_email.email_header_blob = tb_other_blob.blob_name OR
  11.       tb_email.email_footer_blob = tb_other_blob.blob_name
This will return identical rows except for the last column which obviously differs.
What can I do to retrieve the same results in one row with both blob_text columns added to the remaining columns.

I dont know if I explained myself well enough
Please let me know if you have any problems understanding I'll gladly try to explain in more detail

Any help is greatly appreciated
Thanks
Luk
Jun 13 '08 #1
2 1985
Atli
5,058 Expert 4TB
You could try something like:
Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.   a.Col1, a.Col2 
  3.   b1.text AS 'header_text',
  4.   b2.text AS 'footer_text'
  5. FROM tblA AS a
  6. INNER JOIN tblB AS b1
  7.   ON b1.fk = a.pk
  8.   AND b1.name = a.header_name
  9. INNER JOIN tblB AS b2
  10.   ON b2.fk = a.pk
  11.   AND b2.name = a.footer_name
  12.  
It should JOIN the tblB table twice, but as two separate tables.
Jun 14 '08 #2
pechar
56
You could try something like:
Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.   a.Col1, a.Col2 
  3.   b1.text AS 'header_text',
  4.   b2.text AS 'footer_text'
  5. FROM tblA AS a
  6. INNER JOIN tblB AS b1
  7.   ON b1.fk = a.pk
  8.   AND b1.name = a.header_name
  9. INNER JOIN tblB AS b2
  10.   ON b2.fk = a.pk
  11.   AND b2.name = a.footer_name
  12.  
It should JOIN the tblB table twice, but as two separate tables.
Now you are a real life saver!!
Thank a lot it worked perfectly. Why didn't I think of this its so simple.

Thanks again
Luk
Jun 16 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Lenz Grimmer | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, MySQL 4.0.14, a new version of the popular Open Source/Free Software Database, has been released. It is now available in source and binary...
0
by: Philip Stoev | last post by:
Hi all, Please tell me if any of this makes sense. Any pointers to relevant projects/articles will be much appreciated. Philip Stoev http://www.stoev.org/pivot/manifest.htm ...
0
by: Eric B. | last post by:
Hi, I'm somewhat new to MySql. I've been using it for a while, but pretty much out of the box setup, and am starting to suffer heavily with my larger tables. I have a table with 5,000,000+...
0
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest...
11
by: DJJ | last post by:
I am using the MySQL ODBC 3.51 driver to link three relatively small MySQL tables to a Microsoft Access 2003 database. I am finding that the data from the MySQL tables takes a hell of a long time...
12
by: jaYPee | last post by:
I have currently using a dataset to access my data from sql server 2000. The dataset contains 3 tables that is related to each other. parent/child/grandchild relationship. My problem is it's very...
7
by: Peter | last post by:
Gday, I have a dataset with multiple tables, from which I want to access a single value in one of those tables. I know I can do something like: Decimal myVar =...
4
by: Federico | last post by:
Hi everybody, I'm evaluating the possibility of using MySQL 5.0 as a database backend for an application we are doing and I'd like to have hardware requirements rough estimates and/or real world...
6
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.