Solved

mysql real data plus rows from matrix

Posted on 2003-12-08
8
476 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
ID: 9898758
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
ID: 9899299
.. 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
ID: 9899390
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
ID: 9899902
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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 2

Accepted Solution

by:
David Little earned 220 total points
ID: 9900041
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
ID: 9900841
.. 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
ID: 9901407
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
ID: 9901681
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Is there a canned solution for a custom genealogy website 2 79
Write Caching in the Cloud for VFP9 16 81
Online Forms 6 52
Steps to produce a data strategy 5 35
Read about achieving the basic levels of HRIS security in the workplace.
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
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…

943 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

18 Experts available now in Live!

Get 1:1 Help Now