samedi 25 septembre 2021

MySQL: A simple, but odd "join" between two tables

This is so simple, I'm embarrassed to have to ask the question:

I'm trying to return a single row consisting of an element from a column from table A and an element from a column from table B, chosen at random. I've never done such a thing and I'm stumped. There is no relationship between the two tables (intentionally). I can issue the commands individually and get what I desire, but I can't put them together in a single query. A UNION between the two queries (my first thought) results in TWO rows, but I desire a single row. Any thoughts?

    (SELECT column_a FROM table_x ORDER BY rand() LIMIT 1) 
       UNION
    (SELECT column_b FROM table_y ORDER BY rand() LIMIT 1)


    DESIRED OUTPUT:
                  column_a        column_b
                  ---------------------------
    Row 1:        x.element_a     y.element_b

Each individual query performs as expected (albeit costly). Joining the two without any common key is the problem.




Aucun commentaire:

Enregistrer un commentaire