Join two table and fetch data!

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
nshthmistry
Forum Newbie
Posts: 3
Joined: Fri Feb 28, 2014 5:33 am

Join two table and fetch data!

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Join two table and fetch data!

Post 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.
(#10850)
nshthmistry
Forum Newbie
Posts: 3
Joined: Fri Feb 28, 2014 5:33 am

Re: Join two table and fetch data!

Post 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..
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Join two table and fetch data!

Post 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";
(#10850)
Post Reply