Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

mysql real data plus rows from matrix

Posted on 2003-12-08
8
Medium Priority
?
541 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

783 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