How to select values from two different tables in SQL

后端 未结 7 1432
醉梦人生
醉梦人生 2021-02-08 16:15

I have two tables in my SQL Server database. The first is Test1 and second is Test2. There is a column RegNo in both tables.

Now I

相关标签:
7条回答
  • 2021-02-08 16:31
    SELECT Test1.SurName, Test2.Class, Test2.Medium
    FROM Test1
    INNER JOIN Test2
    ON Test1.RegNo = Test2.RegNo
    

    please see a visual explanation of joins this is very helpful in learning joins.

    0 讨论(0)
  • 2021-02-08 16:31
    SELECT Test1.SurName, Test2.Class, Test2.Medium
    FROM Test1 
    INNER JOIN Test2 ON Test1.RegNo = Test2.RegNo
    
    0 讨论(0)
  • 2021-02-08 16:33

    Very Basic question, try google next time and this now:

    SELECT Test1.SurName, Test2.Class, Test2.Medium
    FROM Test1
    inner join Test2 ON Test1.RegNo = Test2.RegNo
    
    0 讨论(0)
  • 2021-02-08 16:36

    Try this:

    SELECT Test1.SurName, Test2.Class, Test2.Medium
    FROM Test1 INNER JOIN Test2
    ON Test1.RegNo = Test2.RegNo
    WHERE Test1.RegNo = desired_id
    
    0 讨论(0)
  • 2021-02-08 16:37

    If you are looking foe method without join and relation.This will do the trick.

    select 
       (
       select s.state_name from state s where s.state_id=3
       ) statename,
       (
       select c.description from country c where c.id=5
       ) countryname
       from dual;   
    

    where dual is a dummy table with single column--anything just require table to view

    0 讨论(0)
  • 2021-02-08 16:44
    select
        Test1.SurName,
        Test2.Class,
        Test2.Medium
    from Test1
    inner join Test2
    on Test1.RegNo = Test2.RegNo
    

    And if you want to select your data for a particular RegNo, just add a WHERE clause to the end, like so:

    select
        Test1.SurName,
        Test2.Class,
        Test2.Medium
    from Test1
    inner join Test2
    on Test1.RegNo = Test2.RegNo
    where Test1.RegNo = 123456   -- or whatever value/datatype your RegNo is
    
    0 讨论(0)
提交回复
热议问题