仅从其他表中获取1个条目

仅从其他表中获取1个条目

This question already has answers here:
How to select unique records by SQL
                                
                                    (8个答案)
                                
                        
                                4年前关闭。
            
                    
我有2个需要连接的表,在这里说明是我的表:

餐桌宝贝

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


表动作

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


我有的:

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


因为我不需要细节,所以这里是我想要的:

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


有人可以帮助我做出正确的查询吗?

最佳答案

如@drew所建议,您可以只使用以下查询(您可能希望确认表名与字段名不冲突):

SELECT DISTINCT baby.id, baby.name, action.action
FROM baby
JOIN action
WHERE baby.action_id=action.id;

关于mysql - 仅从其他表中获取1个条目,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34036356/

10-11 03:22