![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hi everybody, I am new to database programming, started this week! I am using MySQL 5.0 with myphpadmin as the interface to it. Basically I would like to know if there is some way to get a database to perform automatic calculations, for instance, to make things simple, lets say I have two tables - "Products" and "Sales". In my Products table, I have a Primary Key for a field called "Catalogue Number", this is unique to each product. All my product sales are listed in the "Sales" table, where I also have the "Catalogue Number" field, as a Foreign Key. I am using the "Catalogue Number" field to create a relation, so that when I go back to my "Products" table I can get information from the "Sales" table to furnish the field "Total Sold" - in this way I hope to calculate how many of a particular "Catalogue Number" have sold, by examining the relevant fields in the "Sales" table. In spreadsheets I have used, and databases (Lotus Approach), I can use a SUM IF function, to sum a particular column or row if it another specified column or row contains a particular value. My syntax is probably way off, but something allong the following lines is what I am refering to: SUM IF A1-A100 = "RBX001", B1-B100 Cells A1-A100 contain the cell block (column) where the value "RBX001" is searched for, and if there are occurences of that value at A32, A45, A73, A92, & A97, the corresponding cells in cell block (column) B are summed. So could I program a specific field in my databse with a function to do a similar thing, so that I can examine the "Catalogue Number" field in my "Sales" table for a particular value, and if the value occurs, sum (or count) values in other correspnding fields. Anyone know the MySQL function syntax for this if it is possible? I am frantically reading, but so far a lot of it seems not relevant or way over my head! Regards, CB. |
#3
| |||
| |||
|
|
Hi everybody, I am new to database programming, started this week! I am using MySQL 5.0 with myphpadmin as the interface to it. Basically I would like to know if there is some way to get a database to perform automatic calculations, for instance, to make things simple, lets say I have two tables - "Products" and "Sales". In my Products table, I have a Primary Key for a field called "Catalogue Number", this is unique to each product. All my product sales are listed in the "Sales" table, where I also have the "Catalogue Number" field, as a Foreign Key. I am using the "Catalogue Number" field to create a relation, so that when I go back to my "Products" table I can get information from the "Sales" table to furnish the field "Total Sold" - in this way I hope to calculate how many of a particular "Catalogue Number" have sold, by examining the relevant fields in the "Sales" table. In spreadsheets I have used, and databases (Lotus Approach), I can use a SUM IF function, to sum a particular column or row if it another specified column or row contains a particular value. My syntax is probably way off, but something allong the following lines is what I am refering to: SUM IF A1-A100 = "RBX001", B1-B100 Cells A1-A100 contain the cell block (column) where the value "RBX001" is searched for, and if there are occurences of that value at A32, A45, A73, A92, & A97, the corresponding cells in cell block (column) B are summed. So could I program a specific field in my databse with a function to do a similar thing, so that I can examine the "Catalogue Number" field in my "Sales" table for a particular value, and if the value occurs, sum (or count) values in other correspnding fields. Anyone know the MySQL function syntax for this if it is possible? I am frantically reading, but so far a lot of it seems not relevant or way over my head! Regards, CB. I suspect you are looking in the wrong place. MySQL is a |
#4
| |||
| |||
|
|
Cerebral Believer wrote: Hi everybody, I am new to database programming, started this week! I am using MySQL 5.0 with myphpadmin as the interface to it. Basically I would like to know if there is some way to get a database to perform automatic calculations, for instance, to make things simple, lets say I have two tables - "Products" and "Sales". In my Products table, I have a Primary Key for a field called "Catalogue Number", this is unique to each product. All my product sales are listed in the "Sales" table, where I also have the "Catalogue Number" field, as a Foreign Key. I am using the "Catalogue Number" field to create a relation, so that when I go back to my "Products" table I can get information from the "Sales" table to furnish the field "Total Sold" - in this way I hope to calculate how many of a particular "Catalogue Number" have sold, by examining the relevant fields in the "Sales" table. In spreadsheets I have used, and databases (Lotus Approach), I can use a SUM IF function, to sum a particular column or row if it another specified column or row contains a particular value. My syntax is probably way off, but something allong the following lines is what I am refering to: SUM IF A1-A100 = "RBX001", B1-B100 Cells A1-A100 contain the cell block (column) where the value "RBX001" is searched for, and if there are occurences of that value at A32, A45, A73, A92, & A97, the corresponding cells in cell block (column) B are summed. So could I program a specific field in my databse with a function to do a similar thing, so that I can examine the "Catalogue Number" field in my "Sales" table for a particular value, and if the value occurs, sum (or count) values in other correspnding fields. Anyone know the MySQL function syntax for this if it is possible? I am frantically reading, but so far a lot of it seems not relevant or way over my head! Regards, CB. For example you can put an expression like this into a view: (CASE WHEN col1 = 'RBX001' THEN col2 + col3 ELSE 0 END) http://dev.mysql.com/doc/refman/5.0/...functions.html Try not to conceptualise in spreadsheet terms. A relational table is nothing like a spreadsheet, except in the superficial sense that both are often visualised as a grid. -- David Portas, SQL Server MVP Whenever possible please post enough code to reproduce your problem. Including CREATE TABLE and INSERT statements usually helps. State what version of SQL Server you are using and specify the content of any error messages. SQL Server Books Online: http://msdn2.microsoft.com/library/m...S,SQL.90).aspx -- |
#5
| |||
| |||
|
|
He is using mysql not sqlserver and it is version 5.0 |
#6
| |||
| |||
|
|
Jim Kennedy wrote: He is using mysql not sqlserver and it is version 5.0 What I posted was standard SQL92 and according to the MySQL docs CASE is supported by version 5.0. I haven't tested it myself though. |
#7
| |||
| |||
|
|
Cerebral Believer wrote: Hi everybody, I am new to database programming, started this week! I am using MySQL 5.0 with myphpadmin as the interface to it. Basically I would like to know if there is some way to get a database to perform automatic calculations, for instance, to make things simple, lets say I have two tables - "Products" and "Sales". In my Products table, I have a Primary Key for a field called "Catalogue Number", this is unique to each product. All my product sales are listed in the "Sales" table, where I also have the "Catalogue Number" field, as a Foreign Key. I am using the "Catalogue Number" field to create a relation, so that when I go back to my "Products" table I can get information from the "Sales" table to furnish the field "Total Sold" - in this way I hope to calculate how many of a particular "Catalogue Number" have sold, by examining the relevant fields in the "Sales" table. In spreadsheets I have used, and databases (Lotus Approach), I can use a SUM IF function, to sum a particular column or row if it another specified column or row contains a particular value. My syntax is probably way off, but something allong the following lines is what I am refering to: SUM IF A1-A100 = "RBX001", B1-B100 Cells A1-A100 contain the cell block (column) where the value "RBX001" is searched for, and if there are occurences of that value at A32, A45, A73, A92, & A97, the corresponding cells in cell block (column) B are summed. So could I program a specific field in my databse with a function to do a similar thing, so that I can examine the "Catalogue Number" field in my "Sales" table for a particular value, and if the value occurs, sum (or count) values in other correspnding fields. Anyone know the MySQL function syntax for this if it is possible? I am frantically reading, but so far a lot of it seems not relevant or way over my head! Regards, CB. I suspect you are looking in the wrong place. MySQL is a database engine. The user interface is the SQL language which can be invoked directly from the command line or by embedding SQL commands in programs written in a variety of languages. What you want can be accomplished using SQL along the lines of: select catalogue_number, count(*) from sales group by catalogue_number; BTW, when talking about RDBMS we do not have fields and records. We have columns and rows. I suspect this is because SQL operations apply to complete sets of data rather than individual data items. The sooner you start thinking about your tables as collections or sets rather than individual records the sooner you will become proficient using SQL. |
#8
| |||
| |||
|
|
"Jerry Gitomer" <jgitomer (AT) verizon (DOT) net> wrote in message news:vsTOg.2516$2P3.1852 (AT) trnddc02 (DOT) .. Cerebral Believer wrote: Hi everybody, I am new to database programming, started this week! I am using MySQL 5.0 with myphpadmin as the interface to it. Basically I would like to know if there is some way to get a database to perform automatic calculations, for instance, to make things simple, lets say I have two tables - "Products" and "Sales". In my Products table, I have a Primary Key for a field called "Catalogue Number", this is unique to each product. All my product sales are listed in the "Sales" table, where I also have the "Catalogue Number" field, as a Foreign Key. I am using the "Catalogue Number" field to create a relation, so that when I go back to my "Products" table I can get information from the "Sales" table to furnish the field "Total Sold" - in this way I hope to calculate how many of a particular "Catalogue Number" have sold, by examining the relevant fields in the "Sales" table. In spreadsheets I have used, and databases (Lotus Approach), I can use a SUM IF function, to sum a particular column or row if it another specified column or row contains a particular value. My syntax is probably way off, but something allong the following lines is what I am refering to: SUM IF A1-A100 = "RBX001", B1-B100 Cells A1-A100 contain the cell block (column) where the value "RBX001" is searched for, and if there are occurences of that value at A32, A45, A73, A92, & A97, the corresponding cells in cell block (column) B are summed. So could I program a specific field in my databse with a function to do a similar thing, so that I can examine the "Catalogue Number" field in my "Sales" table for a particular value, and if the value occurs, sum (or count) values in other correspnding fields. Anyone know the MySQL function syntax for this if it is possible? I am frantically reading, but so far a lot of it seems not relevant or way over my head! Regards, CB. I suspect you are looking in the wrong place. MySQL is a database engine. The user interface is the SQL language which can be invoked directly from the command line or by embedding SQL commands in programs written in a variety of languages. What you want can be accomplished using SQL along the lines of: select catalogue_number, count(*) from sales group by catalogue_number; BTW, when talking about RDBMS we do not have fields and records. We have columns and rows. I suspect this is because SQL operations apply to complete sets of data rather than individual data items. The sooner you start thinking about your tables as collections or sets rather than individual records the sooner you will become proficient using SQL. Thanks for your post Jerry, I got through with: SELECT SUM(`Quantity Ordered`) FROM `Sales Detail Information` WHERE `Catalogue Number` = "FBDCD001R"; I want to see if I can specify that as a function or proceedure so that I can select it from the function selector on the insert sub-tab in myphpadmin. I hope this will enable the column to always hold the current value for Quantity Sold (the sum total of "Quantity Ordered"). |
#9
| |||
| |||
|
|
"Cerebral Believer" <nospamthanks (AT) hadenoughalready (DOT) com> wrote in message news:tlZOg.22908$wo3.11123 (AT) newsfe7-gui (DOT) ntli.net... "Jerry Gitomer" <jgitomer (AT) verizon (DOT) net> wrote in message news:vsTOg.2516$2P3.1852 (AT) trnddc02 (DOT) .. Cerebral Believer wrote: Hi everybody, I am new to database programming, started this week! I am using MySQL 5.0 with myphpadmin as the interface to it. Basically I would like to know if there is some way to get a database to perform automatic calculations, for instance, to make things simple, lets say I have two tables - "Products" and "Sales". In my Products table, I have a Primary Key for a field called "Catalogue Number", this is unique to each product. All my product sales are listed in the "Sales" table, where I also have the "Catalogue Number" field, as a Foreign Key. I am using the "Catalogue Number" field to create a relation, so that when I go back to my "Products" table I can get information from the "Sales" table to furnish the field "Total Sold" - in this way I hope to calculate how many of a particular "Catalogue Number" have sold, by examining the relevant fields in the "Sales" table. |
|
In spreadsheets I have used, and databases (Lotus Approach), I can use a SUM IF function, to sum a particular column or row if it another specified column or row contains a particular value. My syntax is probably way off, but something allong the following lines is what I am refering to: SUM IF A1-A100 = "RBX001", B1-B100 Cells A1-A100 contain the cell block (column) where the value "RBX001" is searched for, and if there are occurences of that value at A32, A45, A73, A92, & A97, the corresponding cells in cell block (column) B are summed. So could I program a specific field in my databse with a function to do a similar thing, so that I can examine the "Catalogue Number" field in my "Sales" table for a particular value, and if the value occurs, sum (or count) values in other correspnding fields. Anyone know the MySQL function syntax for this if it is possible? I am frantically reading, but so far a lot of it seems not relevant or way over my head! Regards, CB. I suspect you are looking in the wrong place. MySQL is a database engine. The user interface is the SQL language which can be invoked directly from the command line or by embedding SQL commands in programs written in a variety of languages. What you want can be accomplished using SQL along the lines of: select catalogue_number, count(*) from sales group by catalogue_number; BTW, when talking about RDBMS we do not have fields and records. We have columns and rows. I suspect this is because SQL operations apply to complete sets of data rather than individual data items. The sooner you start thinking about your tables as collections or sets rather than individual records the sooner you will become proficient using SQL. Thanks for your post Jerry, I got through with: SELECT SUM(`Quantity Ordered`) FROM `Sales Detail Information` WHERE `Catalogue Number` = "FBDCD001R"; I want to see if I can specify that as a function or proceedure so that I can select it from the function selector on the insert sub-tab in myphpadmin. I hope this will enable the column to always hold the current value for Quantity Sold (the sum total of "Quantity Ordered"). OK, I found the answer, again quite simple, but requiring back-tick quotes (which I had not put round the procedure name), which is not specified on the MySQL website, neither in the reference material or the examples. So this works: CREATE PROCEDURE `008_sales`() SELECT SUM(`Quantity Ordered`) FROM `Sales Detail Information` WHERE `Catalogue Number` = "FBDCD008"; Anyone know how I can get a column to automatically run that procedure? Is that even possible? Regards, C.B. |
#10
| |||
| |||
|
|
Cerebral Believer wrote: "Cerebral Believer" <nospamthanks (AT) hadenoughalready (DOT) com> wrote in message news:tlZOg.22908$wo3.11123 (AT) newsfe7-gui (DOT) ntli.net... "Jerry Gitomer" <jgitomer (AT) verizon (DOT) net> wrote in message news:vsTOg.2516$2P3.1852 (AT) trnddc02 (DOT) .. Cerebral Believer wrote: Hi everybody, I am new to database programming, started this week! I am using MySQL 5.0 with myphpadmin as the interface to it. Basically I would like to know if there is some way to get a database to perform automatic calculations, for instance, to make things simple, lets say I have two tables - "Products" and "Sales". In my Products table, I have a Primary Key for a field called "Catalogue Number", this is unique to each product. All my product sales are listed in the "Sales" table, where I also have the "Catalogue Number" field, as a Foreign Key. I am using the "Catalogue Number" field to create a relation, so that when I go back to my "Products" table I can get information from the "Sales" table to furnish the field "Total Sold" - in this way I hope to calculate how many of a particular "Catalogue Number" have sold, by examining the relevant fields in the "Sales" table. Another point of terminology, a TABLE is the implimentation of a relation. When you combine data from two or more tables, it is a JOIN. You really need to step away from your spreadsheet view and read about the concepts of Relational Data Model. You might just start from the beginning, read Codd's paper about this: http://www.acm.org/classics/nov95/toc.html |
|
In spreadsheets I have used, and databases (Lotus Approach), I can use a SUM IF function, to sum a particular column or row if it another specified column or row contains a particular value. My syntax is probably way off, but something allong the following lines is what I am refering to: SUM IF A1-A100 = "RBX001", B1-B100 Cells A1-A100 contain the cell block (column) where the value "RBX001" is searched for, and if there are occurences of that value at A32, A45, A73, A92, & A97, the corresponding cells in cell block (column) B are summed. So could I program a specific field in my databse with a function to do a similar thing, so that I can examine the "Catalogue Number" field in my "Sales" table for a particular value, and if the value occurs, sum (or count) values in other correspnding fields. Anyone know the MySQL function syntax for this if it is possible? I am frantically reading, but so far a lot of it seems not relevant or way over my head! Regards, CB. I suspect you are looking in the wrong place. MySQL is a database engine. The user interface is the SQL language which can be invoked directly from the command line or by embedding SQL commands in programs written in a variety of languages. What you want can be accomplished using SQL along the lines of: select catalogue_number, count(*) from sales group by catalogue_number; BTW, when talking about RDBMS we do not have fields and records. We have columns and rows. I suspect this is because SQL operations apply to complete sets of data rather than individual data items. The sooner you start thinking about your tables as collections or sets rather than individual records the sooner you will become proficient using SQL. Thanks for your post Jerry, I got through with: SELECT SUM(`Quantity Ordered`) FROM `Sales Detail Information` WHERE `Catalogue Number` = "FBDCD001R"; I want to see if I can specify that as a function or proceedure so that I can select it from the function selector on the insert sub-tab in myphpadmin. I hope this will enable the column to always hold the current value for Quantity Sold (the sum total of "Quantity Ordered"). OK, I found the answer, again quite simple, but requiring back-tick quotes (which I had not put round the procedure name), which is not specified on the MySQL website, neither in the reference material or the examples. So this works: CREATE PROCEDURE `008_sales`() SELECT SUM(`Quantity Ordered`) FROM `Sales Detail Information` WHERE `Catalogue Number` = "FBDCD008"; Anyone know how I can get a column to automatically run that procedure? Is that even possible? Regards, C.B. If you get that procedure to run, where do you want the result to go? |
|
Actually, unlike a spreadsheet, you do not want to run this automatically. You probably want to get the SUM() for your report screen. There is no reason to store this value since it can be computed easily when needed, with the advantage that is is correct when needed. So create a function to call or create a VIEW which your report application can use. |
![]() |
| Thread Tools | |
| Display Modes | |
| |