MySQL USING子句

MySQL的JOIN过程中,若两个表JOIN的列名一致,可以采用USING子句来代替ON子句,这样可以使得SQL更简洁,示例如下:

SELECT
    p.date,
    c.`name` as client,
    p.amount,
    pm.`name` as payment_method
FROM clients c
JOIN payments p
    on c.client_id = p.client_id
JOIN payment_methods pm
    on p.payment_id = pm.payment_method_id;

-- 使用USING子句示例
SELECT
    p.date,
    c.`name` as client,
    p.amount,
    pm.`name` as payment_method
FROM clients c
JOIN payments p
    USING(client_id)
JOIN payment_methods pm
    on p.payment_id = pm.payment_method_id;
database
50 views
Comments
登录后评论
Sign In