Solved

mysql real data plus rows from matrix

Posted on 2003-12-08
8
461 Views
Last Modified: 2008-03-10

I want to select all records from table A (historytable)
and append (union) to result
those records from
table B (matrixtable) wich are not present in table A
for particular record (historytable.recordid)

(to let edit fields that have been entered
and provide fields that haven't been entered
from 'matrix' table)

both tables contain fields price, amount and tax (matrixtable - initial values histrorytable - recorded values)

.. i have related field historytable.matrixid for every record in historytable .. (as i take paymentname and other fields from matrixtable using joins)

i am trying to use union query and having problems with duplicate records as it's not allowed to use group by on union queries in mysql.

Maybe you have some ellegant subquery sollution or something like that  by hand ..?
0
Comment
Question by:sspats
  • 4
  • 4
8 Comments
 
LVL 2

Expert Comment

by:David Little
Comment Utility
Will this work?

select matrixid, price, amount, tax
from historytable
union
select matrixid, price, amount, tax
from matrixtable
where maxtrixid not in (select distinct matrixid from historytable)
0
 
LVL 1

Author Comment

by:sspats
Comment Utility
.. well the question was  messy -
but the thing i want to get out is basically the
matrix table in which rows that are present in historytable
(matrixtable.id = historytable.matrixid)
are replaced with those from historytable

dplittle: i'm trying to work on your idea. but till now it's just neverending syntax problems

:/
0
 
LVL 2

Expert Comment

by:David Little
Comment Utility
What kind of syntax errors? What's your platform? The syntax I gave is straight ANSI SQL and should work on most databases.

David
0
 
LVL 1

Author Comment

by:sspats
Comment Utility
after another fifty times of trials and 'error carnival' i've found this :
.. starting with version 4.1, MySQL supports all subquery forms

and compared to this ..
C:\mysql\bin>mysql.exe
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 167 to server version: 4.0.14-nt
probably it's time to upgrade - although
MySQL 4.1 -- is mentioned as Alpha release

dplittle: thanks for hint !
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 2

Accepted Solution

by:
David Little earned 220 total points
Comment Utility
Ahhh ... I see said the blind man ... try this instead (or upgrade <grin>)


select matrixid, price, amount, tax
from historytable
union
select m.matrixid, m.price, m.amount, m.tax
from matrixtable m left outer join historytable h
    on m.matrixid = h.matrixid
where h.maxtrixid is null
0
 
LVL 1

Author Comment

by:sspats
Comment Utility
.. well generaly it works correct, but
it's problematic when i need to do the trick for particular record
 
- i can do it in first part of the union query by adding
WHERE historytable.paymentid = 55

but it seams impossible to tell to the second part that I
need to select all rows but those wich are selected in first part

where (h.maxtrixid is null) OR (h.paymentid!=55)
gives a list of duplicated entries
Adding
Group by m.matrixid
again returns all records that are in matrixtable
0
 
LVL 1

Author Comment

by:sspats
Comment Utility
it's there !
i'll write
HAVING Sum(IF(h.paymentid=55, 1, 0)) = 0;
in the second union query part
0
 
LVL 2

Expert Comment

by:David Little
Comment Utility
I'm glad you got an answer but I think the "C" grade is a little harse ... your initial query had nothing about needed criteria on the selections. In fact, you said "I want to select ALL records from table A..." (emphasis mine)

A grade of "C" usually means that your question wasn't answered ... it seems that your question WAS answered but the question changed and, in this case, I wasn't even given a chance to respond.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Database tuning – How to start and what to tune. This question is frequently asked by many people, both online and offline. There is no hard and fast rule-of-thumb for performance tuning, however, before beginning the tuning process one should a…
Read about achieving the basic levels of HRIS security in the workplace.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now