0

I have 2 Tables that need to be joined, to illustrate here are my tables:

Table baby

    |id|name |action_id|
    |1 |John |1        |
    |2 |Jane |2        |

Table action

    |id|action|details|
    |1 |laugh |hihihi |
    |1 |laugh |hahaha |
    |2 |cry   |huhuhu |

What I have:

    |id|name |action_id|action|
    |1 |John |1        |laugh |
    |1 |John |1        |laugh |
    |2 |Jane |3        |cry   |

As I didn't need the details, here is what I want:

    |id|name |action_id|action|
    |1 |John |1        |laugh |
    |2 |Jane |3        |cry   |

Can someone help me make the right query?

Kelvin
  • 873
  • 2
  • 9
  • 20
  • 2
    Have you looked at the `DISTINCT` keyword? Just add it to the beginning of the query you already have. http://dev.mysql.com/doc/refman/5.7/en/distinct-optimization.html – Sablefoste Dec 02 '15 at 06:36
  • write it up @Sablefoste. Stick a fork in this in 2 min – Drew Dec 02 '15 at 06:38

2 Answers2

0

Here is an ANSI-92 compliant answer to the question:

SELECT DISTINCT b.id, b.name, b.action_id, a.action
FROM baby b INNER JOIN action a ON b.action_id = a.id
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • I thought my problem lies in the joining clause. Thus, I search only regarding the Join topic. Thanks a lot for the help! – Kelvin Dec 02 '15 at 06:46
0

As suggested by @drew, you can just use the following Query (you might want to confirm the names of your tables don't conflict with the field names):

SELECT DISTINCT baby.id, baby.name, action.action 
FROM baby
JOIN action
WHERE baby.action_id=action.id;
Sablefoste
  • 4,032
  • 3
  • 37
  • 58
  • Hmmm.... normally I do. I guess the late hour and the rush to get an answer that works out lead me to this. An interesting read and opinions about it, however: http://stackoverflow.com/questions/334201/why-isnt-sql-ansi-92-standard-better-adopted-over-ansi-89 – Sablefoste Dec 02 '15 at 06:46
  • I thought my problem lies in the joining clause. Thus, I search only regarding the Join topic. Thanks a lot for the help @Sablefoste ! – Kelvin Dec 02 '15 at 06:47