1
tl
Using MySQL Union
  • 2004/7/25 19:38

  • tl

  • Friend of XOOPS

  • Posts: 999

  • Since: 2002/6/23


I am trying to query two tables with similar structures. MySQL union is very handy in combine the queries. But I don't know how I can do a further query on the union table. I checked MySQL manuals and haven't found anything helpful, hope someone could shed a light on it. Thanks.



my sampe query

select a as Col1, b as Col2 from table X union select c,d from table Y

this will generate a combined list with column names Col1 and Col2

What I want is to perform an arithmetical calculation on Col2 group by Col1.

Are there any easy ways doing it or creating a temp table is the only way?

2
tl
Re: Using MySQL Union
  • 2004/7/26 10:44

  • tl

  • Friend of XOOPS

  • Posts: 999

  • Since: 2002/6/23


Anyone?

3
JMorris
Re: Using MySQL Union
  • 2004/7/26 11:55

  • JMorris

  • XOOPS is my life!

  • Posts: 2722

  • Since: 2004/4/11


I wish I could help you, but I'm just getting started with MySQL queries. One place I look for help with general programming issues is the DevShed forums.

Link:
http://forums.devshed.com/

You might want to do a search there.

Here's about twenty pages of posts that came up by doing a quick search for union.

Link:
http://forums.devshed.com/search.php?searchid=374676

Best of luck!

4
tl
Re: Using MySQL Union
  • 2004/7/26 12:28

  • tl

  • Friend of XOOPS

  • Posts: 999

  • Since: 2002/6/23


Quote:

JMorris wrote:
I wish I could help you, but I'm just getting started with MySQL queries. One place I look for help with general programming issues is the DevShed forums.

Link:
http://forums.devshed.com/

You might want to do a search there.

Here's about twenty pages of posts that came up by doing a quick search for union.

Link:
http://forums.devshed.com/search.php?searchid=374676

Best of luck!


Thanks for the pointer. Really appreciate it.
I'll take a look and post it here if I find an answer to it (hope ).


5
tl
Re: Using MySQL Union
  • 2004/8/2 18:06

  • tl

  • Friend of XOOPS

  • Posts: 999

  • Since: 2002/6/23


Update - if anyone follows this thread

untill MySQL stable version 4.2 introduces SubQuery function, temp table file is the only way to do for now. Would love to hear from someone using MySQL 4.1 on the subquery functions.

6
intel352
Re: Using MySQL Union
  • 2004/8/2 19:08

  • intel352

  • Module Developer

  • Posts: 824

  • Since: 2003/11/23


could you give an example of the arithmetical function that you needed to perform?

just wondering, it'd be nice to figure out a workaround without temp table usage

7
tl
Re: Using MySQL Union
  • 2004/8/2 20:42

  • tl

  • Friend of XOOPS

  • Posts: 999

  • Since: 2002/6/23


I have two similar tables tracking buy/sell transactions. I had to use two tables due to a restriction of the top level table structure and specific information on buyers or sellers.

Essential information that I would like to combine from the two tables
Table A (buyer)
purchase_date
purchase_price
transaction_type

Table B (seller)
sale_date
sale_price
transaction_type

The query used to get the info out:
select year(purchase_date) as year, purchase_price as price, transactiuon_type as type from tableA

Once I have added query from tableB either by a union or inserting into a temp table, I would like to do a final query - by year by type

select year, type, count(*), sum(price), avg(price), max(price), min(price) from (the union or the temp table) group by year,type

Right now, I do not even do a temp table - I have created a perm table holding information and do queries manually, as I am still trying to figure out how to output the group-by results in a compact table using smarty.


8
intel352
Re: Using MySQL Union
  • 2004/8/2 20:54

  • intel352

  • Module Developer

  • Posts: 824

  • Since: 2003/11/23


would this work?

SELECT a.purchase_date AS yeara.transaction_type AS typecount(a.*) AS countsum(b.sale_price), avg(b.sale_price), max(b.sale_price), min(b.sale_pricefrom tableA a
LEFT JOIN tableB b ON a
.transaction_type=b.transaction_type
GROUP BY year
,type

9
tl
Re: Using MySQL Union
  • 2004/8/2 21:00

  • tl

  • Friend of XOOPS

  • Posts: 999

  • Since: 2002/6/23


ON a.transaction_type=b.transaction_type

this won't do, as I am trying to combine all records, not filtering out records.

10
intel352
Re: Using MySQL Union
  • 2004/8/2 21:03

  • intel352

  • Module Developer

  • Posts: 824

  • Since: 2003/11/23


ah, so a purchase and a sale are different eh? i saw the similar field name and assumed they were a common value

hmm, back to the drawing board

Login

Who's Online

229 user(s) are online (134 user(s) are browsing Support Forums)


Members: 0


Guests: 229


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: May 31
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits