Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

mysql real data plus rows from matrix

Posted on 2003-12-08
8
Medium Priority
?
531 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 
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
 
LVL 2

Accepted Solution

by:
David Little earned 440 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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

721 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