Solved

mysql real data plus rows from matrix

Posted on 2003-12-08
8
525 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
Database Solutions Engineer FAQs

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller single-server environments.

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

Veeam gives away 10 full conference passes

Veeam is a VMworld 2017 US & Europe Platinum Sponsor. Enter the raffle to get the full conference pass. Pass includes the admission to all general and breakout sessions, VMware Hands-On Labs, Solutions Exchange, exclusive giveaways and the great VMworld Customer Appreciation Part

Question has a verified solution.

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

Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

623 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