How to fetch data from two tables using mysql query with some conditions applied to the second table?

Friday, July 4, 2014

I want to generate a result from a MySql query with below requirement.


Table 1 :



---------------
| nid | type |
---------------
| 1 | forum |
| 2 | forum |
| 3 | forum |
| 4 | forum |
---------------


Table 2



-----------------------
| nid | cid | created |
-----------------------
| 1 | 32 | 123456 |
| 2 | 65 | 123457 |
| 4 | 67 | 123458 |
| 1 | 61 | 123491 |
| 1 | 78 | 123497 |
| 2 | 23 | 123498 |
| 1 | 12 | 123698 |
| 4 | 54 | 132365 |
| 4 | 81 | 135698 |
| 1 | 30 | 168965 |
-----------------------


Now i require result like below. (Condition : I need the nid from first table, smallest cid for the corresponding nid in second table)



--------------
| nid | cid |
--------------
| 1 | 12 |
| 2 | 23 |
| 4 | 67 |
--------------






http://ift.tt/1qu2ggv