Unix Technical Forum

can join table query achieve this feature?

This is a discussion on can join table query achieve this feature? within the MySQL forums, part of the Database Server Software category; --> I have three tables to record the relationship of stores (the examples are not meaningful)----I wonder if join table ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 11:26 AM
newbie
 
Posts: n/a
Default can join table query achieve this feature?

I have three tables to record the relationship of stores (the examples
are not meaningful)----I wonder if join table query can achieve the
following

I want to list all stores that are on '01' relation list in
`table_relation` and (the challenging part) list how many transactions
have happened between '01' and them.

table_store
--------------------------------------
| id | user_name | phone |
---------------------------------------
| 01 | walmart | 12345 |
---------------------------------------
| 02 | target | 23456 |
---------------------------------------
| 03 | bestbuy | 34567 |
---------------------------------------

table_transactions
---------------------------------------
| id | related_id | relation |
---------------------------------------
| 01 | 02 | close |
---------------------------------------
| 01 | 03 | foe |
---------------------------------------
| 03 | 02 | foe |
---------------------------------------
| 02 | 01 | close |
---------------------------------------
| 02 | 03 | close |
---------------------------------------


table_transactions
-------------------------------------------------------------
| tran_id | buyer_id | provider_id | time |
--------------------------------------------------------------
| 001 | 01 | 02 | 2/17/07|
--------------------------------------------------------------
| 002 | 01 | 02 | 2/18/07|
--------------------------------------------------------------
| 003 | 01 | 03 | 2/19/07|
--------------------------------------------------------------
| 003 | 0ï¼’ | 01 | 2/1ï¼™/07|
--------------------------------------------------------------

************************************************** **
select table_store.id, table_store.user_name, table_store.phone,
table_relation.relation from `table_store`, `table_relation` WHERE
table_relation.id = '01' AND table_relation.related_id =
table_store.id;

with the above query, I can only get (half of what I want):
02 target 23456 close
03 bestbuy 34567 foe
**************************************************

**************************************************
What I really want is the above info plus statistics from
table_transactions: i.e., the transactions happend for
--01 as buyer and the other id as provider
--the other id as buyer and 01 as provider.

02, target, 23456, close, 2 (transactions that '01' as buyer and
'02' is provider), 1 (transactions that '02' as buyer and '01' is
provider)
03 bestbuy 34567 foe, 1(transactions that '01' as buyer and '03' is
provider), 0 (transactions that '03' as buyer and '01' is provider )
**************************************************

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 11:26 AM
strawberry
 
Posts: n/a
Default Re: can join table query achieve this feature?

On 14 Aug, 06:37, newbie <mitbb...@yahoo.com> wrote:
> I have three tables to record the relationship of stores (the examples
> are not meaningful)----I wonder if join table query can achieve the
> following
>
> I want to list all stores that are on '01' relation list in
> `table_relation` and (the challenging part) list how many transactions
> have happened between '01' and them.
>
> table_store
> --------------------------------------
> | id | user_name | Â*phone Â*|
> ---------------------------------------
> | 01 | walmart Â* Â*| Â*12345 Â*|
> ---------------------------------------
> | 02 | target Â* Â* Â* | Â*23456 Â*|
> ---------------------------------------
> | 03 | bestbuy Â* Â*| Â*34567 Â*|
> ---------------------------------------
>
> table_transactions
> ---------------------------------------
> | id Â*| Â* related_id | relation |
> ---------------------------------------
> | 01 | Â* Â* Â*02 Â* Â* Â* Â*| Â*close |
> ---------------------------------------
> | 01 | Â* Â* Â*03 Â* Â* Â* Â*| Â*foe Â* Â* |
> ---------------------------------------
> | 03 | Â* Â* Â*02 Â* Â* Â* Â*| Â*foe Â* Â*|
> ---------------------------------------
> | 02 | Â* Â* Â*01 Â* Â* Â* Â*| Â*close Â*|
> ---------------------------------------
> | 02 | Â* Â* Â*03 Â* Â* Â* Â*| Â*close Â*|
> ---------------------------------------
>
> table_transactions
> -------------------------------------------------------------
> | tran_id Â*| Â* buyer_id | provider_id | Â*time Â*|
> --------------------------------------------------------------
> | 001 Â* Â* Â* | Â* 01 Â* Â* Â* Â* | 02 Â* Â* Â* Â* Â* Â*| Â*2/17/07|
> --------------------------------------------------------------
> | 002 Â* Â* Â* | Â* 01 Â* Â* Â* Â* | 02 Â* Â* Â* Â* Â* Â*| Â*2/18/07|
> --------------------------------------------------------------
> | 003 Â* Â* Â* | Â* 01 Â* Â* Â* Â* | 03 Â* Â* Â* Â* Â* Â*| Â*2/1ï¼™/07|
> --------------------------------------------------------------
> | 003 Â* Â* Â* | Â* 0ï¼’ Â* Â* Â* Â* | 01 Â* Â* Â* Â* Â* Â*| Â*2/1ï¼™/07|
> --------------------------------------------------------------
>
> ************************************************** **
> select table_store.id, table_store.user_name, table_store.phone,
> table_relation.relation from `table_store`, `table_relation` WHERE
> table_relation.id = '01' AND table_relation.related_id =
> table_store.id;
>
> with the above query, I can only get (half of what I want):
> 02 target Â* Â*23456 Â*close
> 03 bestbuy 34567 Â*foe
> **************************************************
>
> **************************************************
> What I really want is the above info plus statistics from
> table_transactions: i.e., the transactions happend for
> --01 as buyer and the other id as provider
> --the other id as buyer and 01 as provider.
>
> 02, target, Â* Â*23456, Â*close, Â*2 (transactions that '01' as buyer and
> '02' is provider), 1 (transactions that '02' as buyer and '01' is
> provider)
> 03 bestbuy 34567 Â*foe, 1(transactions that '01' as buyer and '03' is
> provider), Â*0 (transactions that '03' as buyer and '01' is provider )
> **************************************************


Read up on the JOIN syntax

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:49 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com