MySQL Understanding Basic Joins

前端 未结 6 1741
借酒劲吻你
借酒劲吻你 2021-01-06 20:57

I am struggling to understand basic MySQL joins.

Basically I\'ve got 2 tables one with a customer first name and address id in it and another with the actual address.

相关标签:
6条回答
  • 2021-01-06 21:37
    SELECT  a.name, b.address
    FROM    Customer a INNER JOIN AddressList b on a.addressID = b.addressID
    

    To learn more about joins, see the article below,

    • Visual Representation of SQL Joins
    0 讨论(0)
  • 2021-01-06 21:42

    It would be:

    SELECT firstName, address FROM Address As A 
    INNER JOIN Customer as C ON C.addressId=A.addressId
    

    Visual Representation of JOINS

    0 讨论(0)
  • 2021-01-06 21:46

    Lets say that you have following tables:

    Customer(ID, FName, LName, AddressID)
    Address(AddressID, Streeet, HNUmber, City)
    

    This will display customers address instead of AddressID:

    SELECT c.ID, c.Fname, c.LName, a.Street, a.HNumber, a.City
    FROM Customer c, Address a
    WHERE
      c.AddressID = a.AddressID
    
    0 讨论(0)
  • 2021-01-06 21:53

    Put the same 'addressid' in both the name and address tables then join the two as:

    select name, address from customer join addresses 
    on customer.addressid = addresses.addressid;
    
    0 讨论(0)
  • 2021-01-06 21:53

    Assuming that both tables have an address id, you could use

    SELECT firstname, address, from table1 JOIN table2 
    ON table1.addressid = table2.addressid
    
    0 讨论(0)
  • 2021-01-06 21:55

    Your condition is inner join, This is the simplest, most understood Join and is the most common. This query will return all of the records in the left table (Customer) that have a matching record in the right table (address). This Join is written as follows:

     SELECT firstName, address FROM Customer 
     INNER JOIN address ON Customer.addressId=address.addressId
    

    SQL_LIVE_DEMO

    Sample Output :

    FIRSTNAME       ADDRESS
      Bob       45 Somewhere street
    
    0 讨论(0)
提交回复
热议问题