Count 2 columns together in MySQL

Codes here !

Moderators: macek, egami, gesf

vistavision
New php-forum User
New php-forum User
Posts: 5
Joined: Sat May 10, 2014 10:35 am

Count 2 columns together in MySQL

Postby vistavision » Sat May 10, 2014 10:45 am

Hi all,

I have a question and I hope you guys can help me.

I want to count 2 columns with values together and output the result. So for example I have this MySQL table:

ColA | ColB
1 | 2
2 | 4
1 | 6
4 | 1

I want to output this:

Output:
3
6
7
5

Is this possible? I've tried a lot but I can't figure it out. Thanks so much for helping me out!!

Kind regards,
Mark

vistavision
New php-forum User
New php-forum User
Posts: 5
Joined: Sat May 10, 2014 10:35 am

Re: Count 2 columns together in MySQL

Postby vistavision » Mon May 12, 2014 6:26 am

I know how I can count them together with the SUM value but I want to count them together with an if statement so:

if ($_POST['q1'] == "antwoord1") {
$data = mysql_query("SELECT Col1 FROM test") or die(mysql_error());
}

if ($_POST['q2'] == "antwoord1") {
$data = mysql_query("SELECT Col2 FROM test") or die(mysql_error());
}

if ($_POST['q3'] == "antwoord1") {
$data = mysql_query("SELECT Col3 FROM test") or die(mysql_error());
}

And then something like this if q1 and q2 are true (I know the follwing code is wrong):

$data = mysql_query("SELECT Col1+Col3 FROM test") or die(mysql_error());
echo $data;

How can I do this? Can I use variables?

Thanks!
Mark


Return to “mySQL & php coding”

Who is online

Users browsing this forum: No registered users and 1 guest