Important: Please read the Qt Code of Conduct -

[Solved] How to combine multiple QSqlQueryModel?

  • Dear all,

    I have the such database design.

    @USA_Table {name, phone, address}
    England_Table {name, phone, address}
    Norway_Table {name, phone, address}
    India_Table {name, phone, address}

    I would like to search name in all tables, return result must be in
    @Result_Table {name, phone, address}@

    I can't find SQL solution to get such result, how to do to get such result?
    Need to combine multiple QSqlQueryModels in a view?


  • With a UNION SELECT you can combine the tables in one query...

    bq. The SQL UNION is used to combine the results of two or more SELECT SQL statements into a single result. All the statements concatenated with UNION must have the same structure. This means that they need to have the same number of columns, and corresponding columns must have the same or compatible data types (implicitly convertible to the same type or explicitly converted to the same type). The columns in each SELECT statement must be in exactly the same order too.
    This is how a simple UNION statement looks like:
    @SELECT Column1, Column2 FROM Table1
    SELECT Column1, Column2 FROM Table2@
    The column names in the result of a UNION are always the same as the column names in the first SELECT statement in the UNION.

  • Bad design, if you ask me. Somthing like this makes much more sense, at least from a database normalization perspective:

    Countries {ID, Country}
    Contacts {ID, Name, Telefone, Address, CountryID}

    That will make the UNION select Eddy suggested also no longer needed.

Log in to reply