Page 1 of 1

Join two table and fetch data!

Posted: Wed Mar 05, 2014 5:08 am
by nshthmistry
I have two table and want to fetch data from them.

This query working fine:
"Select * from myads join users on myads.id = users.id";

but the problem is I want to use one of the field as counter

For the Single table this works for me:
"SELECT *,(30-DATEDIFF(now(),updateDate)) as counter FROM myads";

Now, my question is how can i combine this two querys.. any help will be helpful.

Re: Join two table and fetch data!

Posted: Wed Mar 05, 2014 11:55 am
by Christopher
Not sure what you mean by "use one of the field as counter"? Seems like you could put the DATEDIFF() constraint in the WHERE. If you want to consolidate values then use GROUP BY.

Re: Join two table and fetch data!

Posted: Wed Mar 05, 2014 10:00 pm
by nshthmistry
okay, i put it different way..

How can i use this query:
"SELECT *,(30-DATEDIFF(now(),updateDate)) as counter FROM myads";

but for the two tables..

Re: Join two table and fetch data!

Posted: Thu Mar 06, 2014 10:07 pm
by Christopher
You mean like:

"SELECT myads.*,(30-DATEDIFF(now(),myads.updateDate)) as counter, othertable.somefield FROM myads JOIN othertable ON myads.id=othertable.myads_id";