Select Multiple Columns From Multiple Tables

后端 未结 3 852
萌比男神i
萌比男神i 2021-01-14 06:18

I\'m a beginner at MySQL and I\'m having a hard time trying to figure out how to solve this problem:

I have two tables with many entries each. Let\'s say these are t

相关标签:
3条回答
  • SELECT a.dt2, b.dt4, b.dt5
    FROM table1 a, table2 b
    WHERE a.dt2 = 'asd'
    LIMIT 0,1;
    

    Ben's answer is good, you can use more tables just by separating them by comma (,) , but if there's relationship between those tables then you should use some Sub Query or JOIN

    0 讨论(0)
  • 2021-01-14 07:20

    Ben's answer solved my similar issue.

    SELECT t1.dt2, t2.dt4, t2.dt5, t2.dt3 #get dt3 data from table2
    FROM table1 t1, table2 t2
    WHERE t1.dt2 = 'asd' AND t2.dt4 = 'qax' AND t2.dt5 = 456
    
    
    | asd | qax | 456 | 3 | 
    

    '3' being the data I require by querying the 'qax', 456 data in table2, otherwise you're specifying exactly what data will be returned from the columns.

    I only had 2 tables to query in my instance, so the AND expression I can get away with using, it probably isn't best practice and there's most likely a better way for matching data from multiple tables.

    EDIT: I've just realised this question is 5 years old.. I hope you achieved what you wanted to by now.

    0 讨论(0)
  • 2021-01-14 07:21
    SELECT a.dt2, b.dt4, b.dt5
    FROM table1 a, table2 b
    WHERE a.dt2 = 'asd'
    LIMIT 0,1;
    
    0 讨论(0)
提交回复
热议问题