SRL Archive - Web Development

SQL help


grats

2012-05-02 08:11:16

MySQL to be exact but doesn't seem to matter in this case


SELECT i.id, i.name, c.datetime, c.s_ID, c.qty, i.price, i.price * c.qty as 'TAmount' FROM cart c INNER JOIN items i ON i.id = c.id WHERE s_ID='$p_id';


Is there a way to sum all the TAmounts in the statement?

similar to how I multiplied price and quantity (qty) together and made them into 'TAmount' I was thinking maybe I could take all the TAmount's and just add them all together

thanks

Frement

2012-05-02 08:28:43

SELECT i.id, i.name, c.datetime, c.s_ID, c.qty, i.price, SUM(i.price * c.qty) as TAmount FROM cart c INNER JOIN items i ON i.id = c.id WHERE s_ID='$p_id';

grats

2012-05-02 11:29:19


I am beyond lost as to why SUMing those removes all the other stuff

I assume because the SUM of price * qty is only one line so it forces itself to only display on a single line?

Kyle Undefined

2012-05-02 11:33:15

It's because of the INNER JOIN, I believe. Try a LEFT.

grats

2012-05-02 11:43:14

From: Kyle Undefined
It's because of the INNER JOIN, I believe. Try a LEFT.


Yea that messes it up for me, I just gave up and did a double query

[PHP]$query1="SELECT i.id, i.name, c.datetime, c.s_ID, c.qty, i.price, i.price * c.qty as 'TAmount' FROM cart c INNER JOIN items i ON i.id = c.id WHERE s_ID='$p_id'";

$query2="SELECT i.id, i.name, c.datetime, c.s_ID, c.qty, i.price, SUM(i.price * c.qty) as 'STAmount' FROM cart c INNER JOIN items i ON i.id = c.id WHERE s_ID='$p_id'";


$result1=mysql_query($query1);

$result2=mysql_query($query2);


$num=mysql_numrows($result1);
$num2=mysql_numrows($result2);

mysql_close();[/PHP]


Done with this final project already put plenty of work in it, thanks for your guys help though… Didn't think about SUMing the two while multiplying, didn't know that worked how it does, but looks like it's up and running

should probably convert to mysqli later so I don't have to do the ugly double query


yepp looks like all pages are working perfectly, thanks!

superuser

2012-05-02 12:02:36

Use PDO or some ORM lib.