1

I'm using a custom combobox class which populates from a database with an SQL query as such:

playerCB->setDatabaseQuery("SELECT player.id, player.name FROM player WHERE player.team_id = " + QString::number(teamID) + " ORDER BY player.name;");

(internally this class keeps all selected values for each returned row).

I would like to add a temporary row to the results where player.id = -1 and player.name = (Custom Player), however these should not exist within the database. It should be done with using SQL queries only as I can't make any changes to the combobox class. How would I go about doing this, would I have to use temporary tables or something?

Rajveer
  • 847
  • 11
  • 28

1 Answers1

5

You can use literal values like this:

SELECT -1 AS id, '' AS name
Marcus Adams
  • 53,009
  • 9
  • 91
  • 143
  • 4
    `+1`, use `UNION ALL` to add this row to your current results. – Peter Lang Mar 26 '14 at 12:39
  • 1
    Excellent, thank you both. My query now looks like: `"SELECT player.id, player.name FROM player WHERE player.team_id = " + QString::number(teamId) + " UNION ALL SELECT -1 AS id, '' AS name ORDER BY name;"` – Rajveer Mar 26 '14 at 13:36
  • Difference `UNION` vs `UNION ALL`: https://stackoverflow.com/questions/49925/what-is-the-difference-between-union-and-union-all -- Therefore, likely, use `UNION ALL` if you know anyway that the add-hoc added row will not be returned in the normal select, and you do not want to have any performance hit. – juanmirocks Jul 02 '19 at 15:48