Monday, 9 September 2013

selecting data from multiple tables

selecting data from multiple tables

I have this code, and it runs very well. I have problem on how to select
from another table. This code shows how to select and sum() from 2
different tables. What do I do if I am going to select from another table
called tblproductlist. I am selecting 3 tables in one query. I don't know
how to do it. I am searching codes, but it's hard for me to apply.
$result = mysql_query("SELECT (
SELECT SUM(s.total)-SUM(r.total) FROM rsales AS s WHERE r.pcode=s.pcode
) as total, r.pcode
FROM rreturn AS r
GROUP BY r.pcode;");
I tried this code but it returns an error.
$result = mysql_query("SELECT productlist.*,
(SELECT SUM(s.total)-SUM(r.total)
FROM rsales AS s
WHERE r.pcode=s.pcode) as total,
r.pcode FROM rreturn AS r
GROUP BY r.pcode;");
for example I have this ff value.
tblproductlist
pcode | pname |
111 | wire |
tablersales
| total | pcode |
| 200 | 111 |
| 200 | 111 |
tablerreturn
| total | pcode |
| 200 | 111 |
so the output after subtracting its total must be something like this.
pcode | pname | total
111 | wire | 200 |

No comments:

Post a Comment