mysql selecting data from two tables

Dear Experts,
I use PHP and MySql,
I need to select the max mounth data from table1 however, I already have a left join.
I confused, How can I do that?


SELECT table1.bk,table1.nk,table1.ay, table2.isim
FROM table1
LEFT JOIN table2 ON table1.bk = table2.uid
[b]WHERE mounth =  '(I'd likte to select the max(mounth data of the table1 where yil='2018')' [/b]
GROUP BY bk

Open in new window

LVL 1
BRMarketingAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
max of table1.mounth i guess? then try:

SELECT table1.bk, table1.nk, table1.ay, table2.isim
FROM (select max(mounth) mounth from table1 where yil='2018' ) table1
LEFT JOIN table2 ON table1.bk = table2.uid
WHERE table2.mounth =  table1.mounth
GROUP BY table1.bk, table1.nk, table1.ay, table2.isim

Open in new window

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
some amendment to the code:

SELECT table1.bk, table1b.nk, table1b.ay, table2.isim
FROM (select max(mounth) mounth from table1 where yil='2018' ) table1
LEFT JOIN table1 table1b ON table1.bk = table1b.bk
LEFT JOIN table2 ON table1.bk = table2.uid
WHERE table2.mounth =  table1.mounth
GROUP BY table1.bk, table1b.nk, table1b.ay, table2.isim

Open in new window

BRMarketingAuthor Commented:
there is an error message:
#1066 - Not unique table/alias: 'table1b'
Active Protection takes the fight to cryptojacking

While there were several headline-grabbing ransomware attacks during in 2017, another big threat started appearing at the same time that didn’t get the same coverage – illicit cryptomining.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
what if trying this? sorry that don't have the MySQL in my working environment to test now.

SELECT table0.bk, table1.nk, table1.ay, table2.isim
FROM (select max(mounth) mounth from table1 where yil='2018' ) table0
LEFT JOIN table1 ON table0.bk = table1.bk
LEFT JOIN table2 ON table0.bk = table2.uid
WHERE table2.mounth =  table1.mounth
GROUP BY table0.bk, table1.nk, table1.ay, table2.isim

Open in new window

BRMarketingAuthor Commented:
I think we will get the same error. Because there is no table with the name table0 and we did not define it as alias.
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
try this:

SELECT table1.bk, table1.nk, table1.ay, table2.isim
FROM (select max(mounth) mounth from table1 where yil='2018' ) as table0
LEFT JOIN table1 ON table0.mounth = table1.mounth
LEFT JOIN table2 ON table1.bk = table2.uid
WHERE table2.mounth =  table1.mounth
GROUP BY table1.bk, table1.nk, table1.ay, table2.isim

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BRMarketingAuthor Commented:
that is perfect.
thank you very much
Olaf DoschkeSoftware DeveloperCommented:
You mean month? Is that stored in a column named table1.month? Or part of a date column? What's the table1 column named, of which you can get the month, you only gave a yil field containing year (as string?) It's recommendable to store dates in the form of data type date and use functions like year() and month()

A join doesn't hinder you at all to fetch only latest month data.

Creating sample data
Create Table table1 
   (id int AUTO_INCREMENT, t1_data char(10), date_entered date, PRIMARY KEY (id));
Create Table table2 
   (id int AUTO_INCREMENT, table1id int, t2_data char(10), PRIMARY KEY (id));

Insert Into table1 (t1_data, date_entered) Values ('testing','2018-01-04');
Insert Into table1 (t1_data, date_entered) Values ('hello,','2018-02-01');
Insert Into table1 (t1_data, date_entered) Values ('world!','2018-02-10');

Insert Into table2 (table1id, t2_data) Values (1,'x');
Insert Into table2 (table1id, t2_data) Values (2,'y');
Insert Into table2 (table1id, t2_data) Values (3,'z');

Open in new window

notice t1_data, and t2_data as placeholders for all other data colums you have in these tables.

Querying max month data of table1 joined to table2:
Select t1.id as t1id, t2_data, t1.date_entered, t2_data From table1 as t1
Inner join table2 as t2 on t2.table1id = t1.id
Inner join (select max(date_entered) as maxdate from table1) as the on 1=1
Where year(t1.date_entered)=year(the.maxdate) 
and month(t1.date_entered)=month(the.maxdate)

Open in new window


Also see http://sqlfiddle.com/#!9/1ab791/2

Bye, Olaf.
BRMarketingAuthor Commented:
Dear Olaf,
thank you very much for your comment. I find all your comments very helpful. I learned a lot from you. Best regards.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.