David Blomstrom wrote:

--- Roger Baklund <[EMAIL PROTECTED]> wrote:


use test;


Yikes - you lost me on the second word!

Are you talking about the sort of "test" that's
described on this page?:

http://dev.mysql.com/doc/mysql/en/running_mysqltest.html

No. "use test" tells mysql to work in the test database. You may create different, separate databases. One for each project, perhaps.


create table pct (red int,blue int,gray int);
insert into pct values (3,3,4),(10,10,5);

OK, you want me to create a table named "pct" and give it files designated int and named red, blue and gray, right?

No, he's giving you an example which produces the example output you specified in your post. From the top:


Tell mysql to work in the database named test:

  use test;

Create a table named pct with columns named red, blue, and gray:

  create table pct (red int,blue int,gray int);

Add 2 rows to the pct table with specified values:

  insert into pct values (3,3,4),(10,10,5);

Select data from the pct table, displaying percentages instead of raw numbers:

  select @total:=red+blue+gray as total,
    concat(red/(@total/100),"%") as red,
    concat(blue/(@total/100),"%") as blue,
    concat(gray/(@total/100),"%") as gray
  from pct;

For each row, @total:=red+blue+gray adds the values in the 3 columns red, blue, and gray, and stores the result in the user variable @total.

red/(@total/100) is mathematically equivalent to 100*red/@total, which is the red amount as a percent of the total.

concat(100*red/@total,"%") tacks a percent sign on the end.

Try it yourself.  In the mysql client, it should look like this:

mysql> use test;
Database changed
mysql> create table pct (red int,blue int,gray int);
Query OK, 0 rows affected (0.19 sec)

mysql> insert into pct values (3,3,4),(10,10,5);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select @total:=red+blue+gray as total,
    ->   concat(red/(@total/100),"%") as red,
    ->   concat(blue/(@total/100),"%") as blue,
    ->   concat(gray/(@total/100),"%") as gray
    -> from pct;
+-------+------+------+------+
| total | red  | blue | gray |
+-------+------+------+------+
|    10 | 30%  | 30%  | 40%  |
|    25 | 40%  | 40%  | 20%  |
+-------+------+------+------+
2 rows in set (0.00 sec)

The idea now is to adapt this to your data. From this example, you should be able to write a similar SELECT statement using the columns in your table. Also, if you already have a column with the correct totals, you can use it in place of the user variable. Something like:

  SELECT Name, Seat, ...
         CONCAT(100*Native/Pop,"%") AS Native,
         CONCAT(100*White/Pop,"%") AS White,
         ...
  FROM yourtable;

You'll have to use the actual names of your table and columns, of course.

I'd suggest you got it right in your earlier email:

So I suspect this would all make more sense if I had
never begun with Dreamweaver. Some people say
Dreamweaver doesn't bloat the code that much, but it
sure doesn't resemble the example you showed me!

By hiding how things work, Dreamweaver is, well, hiding how things work. I'd suggest you start by working directly with the mysql client to get a handle on sql queries, then work with php to add the web interface.


There are a lot of web tutorials on using mysql and php. For example, there are several on Dev Shed <http://www.devshed.com/>. A quick Google search on "php mysql tutorial" yields numerous hits. I'd suggest you try a few to get a better idea of what's what.

Michael


-- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe: http://lists.mysql.com/[EMAIL PROTECTED]



Reply via email to