July 31, 2011

zither zither
Lab Rat
147 posts

[Solved] How to combine multiple QSqlQueryModel?

 

Dear all,

I have the such database design.

  1. USA_Table {name, phone, address}
  2. England_Table {name, phone, address}
  3. Norway_Table {name, phone, address}
  4. India_Table {name, phone, address}
  5. ..
  6. ..
  7. ..

I would like to search name in all tables, return result must be in

  1. 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?

Thanks

2 replies

July 31, 2011

Eddy Eddy
Area 51 Engineer
1295 posts

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

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:

  1. SELECT Column1, Column2 FROM Table1
  2. UNION
  3. 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.

 Signature 

Qt Certified Specialist
Qt Ambassador

July 31, 2011

Andre Andre
Area 51 Engineer
6031 posts

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.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

 
  ‹‹ [solved] Multiple Lines of text in QToolbox buttons      QSettings how to save QMap<QString,int> into configuration file ››

You must log in to post a reply. Not a member yet? Register here!