How can I get data for one field from multiple tables?

Andy

I have a column ContentID in a table that identifies content that exists in other tables. For example, it could refer to a pageID, productID, etc. My plan is to use that to pull through other information I need, such as a page name or product name. This is what I have so far:

SELECT TL.ID, TL.TableName, TL.FileName, TL.ContentID, p.PageName AS Content
FROM TranslationLog TL
LEFT JOIN Pages P ON TL.ContentID = P.PageID
LEFT JOIN Categories C ON TL.ContentID = C.CategoryID
LEFT JOIN ProductDescriptions PD ON TL.ContentID = PD.SKU 

The idea is for each row, I want to get the data for the specified content using the TableName and ContentID fields. Currently, I'm able to get PageName by selecting p.PageName AS Content. However, I'd like to do this for each of the tables; if the row corresponds to the pages table, then query that table - same for categories and product descriptions. I also need it to have the alias "Content", regardless of which field from another table we're using, such as PageName or ProductName.

Is it possible to do this?

EDIT:

The solution posted by rd_nielsen was almost perfect, but it turned out there was actually a bit of overlap with the ContentID. Here's what I ended up with to fix it:

SELECT TL.ID, TL.TableName, TL.FileName, TL.ContentID, coalesce(P.PageName, C.CategoryName, PD.ProductName)
FROM TranslationLog TL
LEFT JOIN Pages P ON TL.ContentID = P.PageID AND TL.TableName = 'Pages'
LEFT JOIN Categories C ON TL.ContentID = C.CategoryID AND TL.TableName = 'Categories'
LEFT JOIN ProductDescriptions PD ON TL.ContentID = PD.SKU AND TL.TableName = 'Products'
rd_nielsen

If the values of TranslationLog.ContentID can appear in only one of the related tables, then you can coalesce the values from those tables:

SELECT 
    TL.ID, 
    TL.TableName, 
    TL.FileName, 
    TL.ContentID, 
    coalesce(p.PageName, C.CategoryName, PD.ProductName) AS Content
FROM
    ...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I insert data from two tables into one?

From Dev

How can I get multiple json data from json file one by one?

From Dev

Mysql : how i can get 3 columns from multiple tables?

From Dev

How can I query data from multiple tables and sort by time?

From Dev

How can I query data from multiple tables and sort by time?

From Dev

React/Material-UI: How can I map multiple tables from a JSON object with more than one array of data?

From Dev

How can I display data from multiple models in one view?

From Dev

How do I get one column from multiple tables to join into one list?

From Dev

how to insert but get data from multiple tables

From Dev

How to get data from multiple tables?

From Dev

Can I use one select statement to get multiple rows from 2 tables with FK

From Dev

How to get data from multiple tables from single sqlite database in one query for iOS application?

From Dev

How to get data from multiple tables from single sqlite database in one query for iOS application?

From Dev

How can I get data from multiple Siemens PLCs into Excel?

From Dev

Firebase Cloud Functions JavaScript - How to get multiple data from different tables in one function

From Dev

How do I display data from one mySQL table into multiple tables in php?

From Dev

MySQL How can I select data from two tables so that rows from the second one overwrote those from the first one?

From Dev

How can I get data from array data field from Cloud Firestore?

From Dev

How can I query data from multiple tables connected through foreign keys?

From Dev

SQL Basics: How to get details from multiple tables in one query?

From Dev

How to display data from multiple tables in one line in rails

From Dev

How can I move a field from one model to another, and still retain the data?

From Dev

How can I parse one field into multiple fields in the same table?

From Dev

How can I insert multiple row data into two tables simultaneously

From Dev

How can I select from different tables in one MySQL query?

From Dev

How can fetch data from multiple tables in SQLite in Android Studio

From Dev

How to Get data of specific cell from DataGrid that fill by multiple tables

From Dev

how to get data from multiple tables in sql using php

From Dev

How to get rows of Data from Multiple Tables using LinQ To Entities

Related Related

  1. 1

    How can I insert data from two tables into one?

  2. 2

    How can I get multiple json data from json file one by one?

  3. 3

    Mysql : how i can get 3 columns from multiple tables?

  4. 4

    How can I query data from multiple tables and sort by time?

  5. 5

    How can I query data from multiple tables and sort by time?

  6. 6

    React/Material-UI: How can I map multiple tables from a JSON object with more than one array of data?

  7. 7

    How can I display data from multiple models in one view?

  8. 8

    How do I get one column from multiple tables to join into one list?

  9. 9

    how to insert but get data from multiple tables

  10. 10

    How to get data from multiple tables?

  11. 11

    Can I use one select statement to get multiple rows from 2 tables with FK

  12. 12

    How to get data from multiple tables from single sqlite database in one query for iOS application?

  13. 13

    How to get data from multiple tables from single sqlite database in one query for iOS application?

  14. 14

    How can I get data from multiple Siemens PLCs into Excel?

  15. 15

    Firebase Cloud Functions JavaScript - How to get multiple data from different tables in one function

  16. 16

    How do I display data from one mySQL table into multiple tables in php?

  17. 17

    MySQL How can I select data from two tables so that rows from the second one overwrote those from the first one?

  18. 18

    How can I get data from array data field from Cloud Firestore?

  19. 19

    How can I query data from multiple tables connected through foreign keys?

  20. 20

    SQL Basics: How to get details from multiple tables in one query?

  21. 21

    How to display data from multiple tables in one line in rails

  22. 22

    How can I move a field from one model to another, and still retain the data?

  23. 23

    How can I parse one field into multiple fields in the same table?

  24. 24

    How can I insert multiple row data into two tables simultaneously

  25. 25

    How can I select from different tables in one MySQL query?

  26. 26

    How can fetch data from multiple tables in SQLite in Android Studio

  27. 27

    How to Get data of specific cell from DataGrid that fill by multiple tables

  28. 28

    how to get data from multiple tables in sql using php

  29. 29

    How to get rows of Data from Multiple Tables using LinQ To Entities

HotTag

Archive