Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SQL Combine data from two tables

How to combine two tables in SQL?

Suppose we have table called books

book_id    author_id       name
_______    _________       _____________
1          2               XYZ
2          1               ABC

And we have table called authors

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

author_id        firstname        surname
___________      ____________     ___________
1                Alex             Woodman
2                Steve            Bush

I want to combine books and authors in select query:

book_id       author_id     name          author_name
_________     __________    __________    ______________
1             2             XYZ           Steve Bush
2             1             ABC           Alex Woodman

>Solution :

you could use A join statement to combine the two tables. And use a concat function to join the author name columns

SELECT 
    b.book_id, 
    a.author_id, 
    b.name, 
    CONCAT( firstname, ' ',surname) as author_name
FROM books b 
JOIN author a on b.author_id = a.author_id
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading