0

What does

SELECt 2 type, c.*, p.name 
FROM customer c 
LEFT JOIN product p ON (c.id = p.cid) WHERE c.id='5' 
GROUP BY p.name 

return?

What does the 2 in the statement represent

Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55
  • 3
    is literal value.. the query show 2 as result for the first column ... named with alias type – ScaisEdge Jan 22 '18 at 09:04
  • 2
    this is to return a fixed value as `type` column, more about that in [here](https://dev.mysql.com/doc/refman/5.7/en/literals.html), this is called literal values as @scaisEdge had mentioned – hassan Jan 22 '18 at 09:05
  • 1
    I always use the optional keyword AS for readability. "SELECT 2 AS type" looks way better – Giuseppe Jan 22 '18 at 09:08
  • Thank you guys, so another question, In what cases would one want to add an extra column with a value 2? – Ntiyiso Rikhotso Jan 22 '18 at 09:18
  • This can streamline the outputting process by delivering all data from a single source (`$resultset`) rather than some from here and some from there. You can just loop the resultset. Here is one use case: https://stackoverflow.com/a/34758622/2943403 that makes the resultset handling simpler. – mickmackusa Jan 22 '18 at 09:28
  • This has nothing to do with PHP or PHP's mysqli. – Ulrich Eckhardt Jan 22 '18 at 09:58

1 Answers1

3

It just adds an additional column to the result set before other columns. The value of this column is always 2. And a type alias is given to it.

walter
  • 1,199
  • 7
  • 13