1

I have two queries that I am combining using a UNION statement. I know that I need both queries to get the same number and type of arguments to work.

I have one query that only really needs three fields and one that needs four.

Eg query 1's select line looks like this

->select ($db->quoteName(array('c.event','b.date','b.type','a.mob')))

query 2's select line looks like this, XX denotes where I need to insert a dummy field:

->select ($db->quoteName(array('b.event','a.date','a.type', XX)))

What the syntax for putting a dummy field into the select statement of the second query please?

0

I haven't tested it (short on time), but I think this should add a dummy column b.mob which is filled with string 'dummy'.

->select ($db->quoteName(array('b.event','a.date','a.type')))
->select ($db->quote('dummy'). ' AS ' . $db->quoteName('b.mob'))

The equivalent sql should be

SELECT `b.event`, `a.date`, `a.type`, 'dummy' AS `b.mob`
  • Thanks for your reply. I've tried that but its throwing up an SQL error. Any other wise suggestions? – Hannah Smith Apr 21 '15 at 20:41
  • I've tested it now, and it works for me. Can you post the built query? Maybe there is another error somewhere. Like for example, I believe when you use UNION, the whole schema has to be the same, which includes the names. So b.event UNION a.event will give an SQL error. – user3154108 Apr 22 '15 at 7:15
  • Hi, I tried ->select ($db->quote('dummy') .'AS'. $db->quote('mob')) instead and it worked perfectly! Thanks! :0) – Hannah Smith Apr 23 '15 at 19:39
  • The error was likely generated by writing the table alias b into the dummy column name. SQL was probably getting fouled up while trying to verify that the dummy column was a "real" column in the b table. I didn't test, but I would recommend simply removing the table alias b (and delimiting .) and retaining user3154108's suggested $db->qn() since it is the appropriate call for backtick-wrapping a column name. – mickmackusa Jun 19 '18 at 6:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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