
Originally Posted by
guido2004
If you need to know what table each row comes from, just add a column with an identifying value:
Code:
(SELECT
't1' as tablename
, a
FROM t1
WHERE a=10 AND B=1
)
UNION
(SELECT
't2' as tablename
, a
FROM t2
WHERE a=11 AND B=2
)
ORDER BY a LIMIT 10;
I should also add, in this case what would t1 be? Is it an actual column or the actual table? I don't understand how to use this. Let me dump you some of my vitals here.
Table 1 is item_categories
Code:
item_category_id int(11) NO PRI NULL auto_increment
item_type_id int(11) NO MUL NULL
item_category_name varchar(255) NO 0
order_form_category tinyint(1) NO 0
Table 2 is order_forms
Code:
order_form_id int(11) NO PRI NULL auto_increment
order_form_name varchar(255) NO 0
order_form_category tinyint(1) NO NULL
I want to be able to get one result set that has records from both tables that match an order_form_category of 1 that is sorted alphabetically by order_form_name. In order to achieve this I can do a "select item_category_name as order_form_name" but I still need to discern which table the data came from. If I could somehow get the item_type_id from the table item_categories that would be great, but I don't know how to do that.
Here is what I've got so far, but it doesn't work because I am requesting three columns from item_categories and only two from order_forms
Code:
(select item_category_id as order_form_id, item_category_name as order_form_name, item_type_id from item_categories)
UNION
(select order_form_id, order_form_name)
where order_form_category = "1"
Bookmarks