L
L
Lite_robot2016-01-23 23:46:32
MySQL
Lite_robot, 2016-01-23 23:46:32

SQL: how to display 2 columns of the same name in one table, so that one column has names, and the other column has dependencies?

Good day Toaster, developed a database and faced the problem of how to extract data from it, in the desired presentation. I have a database running sqlite. The base has the following structure shown in the picture. Tell me how to display names from the items table in one column and display dependencies in the other using the deps and trades tables in the names view from the items table?
I tried to use the union operator with an alias of the name column from the items table, but the data from name is still displayed in one column.
42300548f45247e1b34bbee3e456bcb8.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
MrTimon, 2017-01-23
@Lite_robot

SELECT 
    deps.itemid as itemid1, tradeid.itemid as itemid2
FROM
    deps
LEFT JOIN 
    tradeid
ON 
   deps.tradeid = tradeid.id

This way you will pull out pairs of IDs of dependencies. Then, if you need something, by making 2 more joins, you can pull out their names.
Something like that:
SELECT 
    items1.name as name1, items2.name as name2
FROM
    (предыдущий запрос) ids
LEFT JOIN items items1 ON items1.id = ids.itemid1 
LEFT JOIN items items2 ON items2.id = ids.itemid2

the idea is to get what you need.

D
Dimonchik, 2016-01-24
@dimonchik2013

www.tutorialspoint.com/sqlite/sqlite_using_joins.htm

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question