On 9 May 2006, at 03:18, Ed Lee wrote:

Hi folks:

Off topic, but I'm wondering if anyone has any idea of how to include table names in the result set of a UNION-like query to a MySQL DB?

I'm running with a query like this:

userRes = lasermgt.SQLSelect("Select * from reservations where fldTimestamp between '" + beforeToday.SQLDate + " 00:00:00' and '" + beforeToday.SQLDate + " 23:59:59' UNION Select * from deletedreservations where fldTimestamp between '" + beforeToday.SQLDate + " 00:00:00' and '" + beforeToday.SQLDate + " 23:59:59' order by fldTimestamp")

Since that's waaaaaay too complicated to follow, I'll clear it up a bit.

This translates into:

Select * from reservations where fldTimestamp between 'SQLDateTime' and 'SQLDateTime' UNION Select * from deletedreservations where fldTimestamp between 'SQLDateTime' and 'SQLDateTime' order by fldTimestamp;

reservations and deletedreservations are two individual tables in my database. They have identical schema.

The idea here is to return a set of results from the two tables that includes a field indicating which table it came from.

Is there an easy way to do this, or so I need to look outside of SQL commands to accomplish it?


Just add a constant string (or integer) as an extra field in your query.

userRes = lasermgt.SQLSelect("Select 'RES' As TabName, * from reservations where fldTimestamp between '" + beforeToday.SQLDate + " 00:00:00' and '" + beforeToday.SQLDate + " 23:59:59' UNION Select 'DEL' As TabName, * from deletedreservations where fldTimestamp between '" + beforeToday.SQLDate + " 00:00:00' and '" + beforeToday.SQLDate + " 23:59:59' order by fldTimestamp")

I've simply modified the above to include a 'RES' and a 'DEL' in each of the query parts and named it "TabName" so it can be picked up easily in your recordset.

--
Ian M. Jones
___________________________________
http://www.imijsoft.com
http://www.ianmjones.net (blog)

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>

Reply via email to