1

I want to know if I can hardcode a value within my select statement. I have the following mysql query that I use to generate a list.

I use concat to build this string as part of creating the list. However, now I need to generate an 'Unknown' record as part of the list.

For example:

20(ABC Object #20)
24(DEF Object #24)

I want to add a value of 'UKNOWN' to the top of the list:

--(UNKNOWN -- )
20(ABC Object #20)
24(DEF Object #24)

Here is what I have so far:

SELECT CAST(p.Serial AS UNSIGNED INTEGER) as Serial, 
p.Object_ID, 
p.Part_ID, 
st.Description AS ObjectDesc, 
s.Object_Num, 
concat(Serial,' (',st.Desc,' #',s.Object_Num,')') as DropList 
FROM Parts p LEFT JOIN Objects s ON p.Object_ID = s.Object_ID 
LEFT JOIN ObjectTypes st ON s.ObjectType_ID = st.ObjectType_ID 

Can I hardcode that string in my select statement? If so, how do I do that?

1
  • 1
    select '--(unknown -- )' union all select ...your query here ...?
    – Marc B
    Feb 2, 2015 at 16:33

1 Answer 1

1

If I understand correctly, you want a new row. You can use UNION

SELECT "" as Serial,
   "" as Object_ID,
   "" as ObjectDEsc,
   "" as Object_Num,
   "--(UNKNOWN -- )" as DropList
UNION
 SELECT CAST(p.Serial AS UNSIGNED INTEGER) as Serial, 
  p.Object_ID, 
  p.Part_ID, 
  st.Description AS ObjectDesc, 
  s.Object_Num, 
  concat(Serial,' (',st.Desc,' #',s.Object_Num,')') as DropList 
 FROM Parts p LEFT JOIN Objects s ON p.Object_ID = s.Object_ID 
 LEFT JOIN ObjectTypes st ON s.ObjectType_ID = st.ObjectType_ID 

In a union query each part must select the same columns. Since your original query included Serial, Object_ID, Part_ID and ObjectDesc, the first part must include it as well.

2
  • since I had a LEFT JOIN condition in my from clause with my original query, do I also need to add that to the top query?
    – noober
    Feb 2, 2015 at 16:48
  • No. They are independent queries.
    – Robbert
    Feb 2, 2015 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.