0

I have these two tables that have nothing in common and I would like a query that gives the results of both but I don't know how to proceed Article table:

id   id_user     article     lang   time
1    1           something   en     327367
2    2           something   en     364756

Product table:

id   id_buyer   id_seler   time_product
1    1          1          463737
2    2          1          487474

Expected result:

id      id_user     article     lang   time     id      id_buyer   id_seler   time_product
1       1           something   en     327367   null    null       null       null
2       2           something   en     364756   null    null       null       null
null    null        null        null   null     1       1          1          463737
null    null        null        null   null     2       2          1          487474

I've read about union but I'm not sure I can apply it here. I would like a page that indifferently lists items and products ordered on time

5
  • The two tables do not have the same number of columns @Frankich Commented Oct 14, 2020 at 9:15
  • 2
    Expected result: Impossible - server will fail because of output column names are duplicated. Commented Oct 14, 2020 at 9:18
  • I can change the column name @Akina Commented Oct 14, 2020 at 9:19
  • 1
    May I ask what this is for? It seems a bit strange to create one result set from two unrelated tables? Why not just have two result sets? Commented Oct 14, 2020 at 9:21
  • I need a page that lists articles and products sorted by date. I could use two queries, one for the products and one for the articles but then I don't know how to sort them by date @MagnusEriksson Commented Oct 14, 2020 at 9:23

1 Answer 1

1
SELECT id t1id, id_user, article, lang, `time` t1time, 
       NULL t2id, NULL id_buyer, NULL id_seler, NULL t2time
FROM table1
UNION ALL
SELECT NULL, NULL, NULL, NULL, NULL, 
       id, id_buyer, id_seler, `time`
FROM table2
/* ORDER BY t1id IS NULL, t1id, t2id */
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.