Sum column values from two MySQL tables

Discuss coding issues, and scripts related to PHP and MySQL.
User avatar
JanMolendijk
Posts: 282
Location: Holland Rotterdam

Sum column values from two MySQL tables

I have this database for my comments
Now i wanna count the comments from citem

Code: Select all

c66u_nucleus_comment

Type	Collatie	Attributen
1	cnumber	int(11)		
2	cbody	text	
3	cuser	varchar(40)	
4	cmail	varchar(100)	
5	cemail	varchar(100)	
6	cmember	int(11)		
7	citem	int(11)		
8	ctime	datetime	
9	chost	varchar(60)	
10	cip	varchar(15)	
11	cblog	int(11)
& those counts from the c66u_nucleus_comment
i wanna place into my items
i think by this inumber

Code: Select all

c66u_nucleus_item

Type	Collatie	Attributen	
1	inumber	int(11)			
2	ititle	varchar(160)	
3	ibody	mediumtext	
4	imore	mediumtext	
5	iblog	int(11)		
6	iauthor	int(11)			
7	itime	datetime		
8	iclosed	tinyint(2)		
9	idraft	tinyint(2)		
10	ikarmapos	int(11)		
11	icat	int(11)			
12	ikarmaneg	int(11)		
13	iposted	tinyint(2)	

Admin Posts: 805
Hello,
1. This is the syntax that you can use to sum numeric values from a column into a mysql table:

Code: Select all

SELECT SUM(col_name) AS snr FROM table_name WHERE some_condition
2. If you want to do multiple SUM(), in multiple tables, and output them in a single SELECT statement with each SUM being a column; you can use a SQL query like this:

Code: Select all

SELECT
(SELECT SUM(col_name1) FROM table_name1) AS snr1,
(SELECT SUM(col_name2) FROM table_name2) AS snr2
FROM table_name1 LIMIT 1
In the rest, i didn't understand what result (count number) you want to get from those tables.
Better post a few rows with data from mysql tables, Not the table structure; and what result you want to be returned.