2010-07-19 43 views
2

我使用兩個表中的UNION ALL創建了一個MySQL視圖,這樣我就可以在這些表中獲得相同數據的相同列名(即tbl1.author2 AS translator ... tbl2.translator AS translator)等等,問題是當我嘗試從該視圖中選擇某些內容時,一半數據以BLOB而不是原始值出現。

視圖定義是:在MySQL視圖中代替正確數據的BLOB

SELECT e.id AS prod_id, 
e.price_vat AS price_vat, 
e.product AS title, 
e.authors AS author, 
e.isbn AS isbn, 
e.ean AS ean, 
e.page_count AS page_count, 
e.publishers AS publishers, 
e.issue_year AS issue_year, 
'e' AS type 
FROM ama_euromedia_products AS e 
UNION ALL 
SELECT 
k.publishers AS publishers, 
DATE_FORMAT(k.publication_date, '%Y') AS issue_year, 
k.ean AS ean, 
k.number_of_pages AS page_count, 
k.author AS author, 
k.isbn AS isbn, 
k.title_full AS title, 
k.price_amount AS price_vat, 
k.internal AS prod_id, 
'k' AS type 
FROM ama_kosmas_products AS k 

表定義:

CREATE TABLE `ama_euromedia_products` (
    `id` int(9) NOT NULL, 
    `product` text COLLATE utf8_czech_ci NOT NULL, 
    `isbn` varchar(255) COLLATE utf8_czech_ci DEFAULT NULL, 
    `authors` text COLLATE utf8_czech_ci, 
    `publishers` text COLLATE utf8_czech_ci, 
    `price` float(9,0) DEFAULT NULL, 
    `price_vat` float(9,0) DEFAULT NULL, 
    `vat` int(3) DEFAULT NULL, 
    `availability` tinyint(1) DEFAULT NULL, 
    `genres` varchar(255) COLLATE utf8_czech_ci DEFAULT NULL, 
    `ean` bigint(14) DEFAULT NULL, 
    `page_count` int(7) DEFAULT NULL, 
    `issue_year` int(4) DEFAULT NULL, 
    `supply_date` timestamp NULL DEFAULT NULL, 
    `width` int(7) DEFAULT NULL, 
    `height` int(7) DEFAULT NULL, 
    `weight` int(7) DEFAULT NULL, 
    `binding` varchar(255) COLLATE utf8_czech_ci DEFAULT NULL, 
    `long_v` text COLLATE utf8_czech_ci, 
    `short` text COLLATE utf8_czech_ci, 
    `imgurl` text COLLATE utf8_czech_ci, 
    `is_preliminary` tinyint(1) DEFAULT NULL, 
    `stack_date` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP, 
    `invoice_only` tinyint(1) DEFAULT NULL, 
    `new` tinyint(1) DEFAULT NULL, 
    `sale` tinyint(1) DEFAULT NULL, 
    `return_v` tinyint(1) DEFAULT NULL, 
    PRIMARY KEY (`id`), 
    KEY `indexy` (`id`,`isbn`,`genres`,`ean`,`issue_year`,`supply_date`,`stack_date`,`new`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci; 


CREATE TABLE `ama_kosmas_products` (
    `id` int(7) NOT NULL AUTO_INCREMENT, 
    `internal` varchar(255) COLLATE utf8_czech_ci DEFAULT NULL, 
    `isbn` varchar(255) COLLATE utf8_czech_ci DEFAULT NULL, 
    `ean` varchar(255) COLLATE utf8_czech_ci DEFAULT NULL, 
    `contained_items` text COLLATE utf8_czech_ci, 
    `title_original` varchar(255) COLLATE utf8_czech_ci DEFAULT NULL, 
    `title_full` varchar(255) COLLATE utf8_czech_ci DEFAULT NULL, 
    `subtitle` varchar(255) COLLATE utf8_czech_ci DEFAULT NULL, 
    `other_text` text COLLATE utf8_czech_ci, 
    `languages` varchar(255) COLLATE utf8_czech_ci DEFAULT NULL, 
    `author` varchar(255) COLLATE utf8_czech_ci DEFAULT NULL, 
    `illustrator` varchar(255) COLLATE utf8_czech_ci DEFAULT NULL, 
    `translator` varchar(255) COLLATE utf8_czech_ci DEFAULT NULL, 
    `number_of_pages` int(7) DEFAULT NULL, 
    `subject_scheme_name` varchar(255) COLLATE utf8_czech_ci DEFAULT NULL, 
    `subject_code` int(7) DEFAULT NULL, 
    `subject_heading_text` varchar(255) COLLATE utf8_czech_ci DEFAULT NULL, 
    `media_file_label` varchar(255) COLLATE utf8_czech_ci DEFAULT NULL, 
    `media_file_thumbnail` varchar(255) COLLATE utf8_czech_ci DEFAULT NULL, 
    `publishers` text COLLATE utf8_czech_ci, 
    `publishing_status` tinyint(2) DEFAULT NULL, 
    `publication_date` timestamp NULL DEFAULT NULL, 
    `product_availability` tinyint(2) DEFAULT NULL, 
    `on_hand` int(7) DEFAULT NULL, 
    `on_order` int(7) DEFAULT NULL, 
    `price_amount` int(7) DEFAULT NULL, 
    `price_tax_rate_code` varchar(1) COLLATE utf8_czech_ci DEFAULT NULL, 
    `price_tax_rate_percent` tinyint(2) DEFAULT NULL, 
    `price_tax_amount` int(7) DEFAULT NULL, 
    `price_taxable_amount` int(7) DEFAULT NULL, 
    `reissue_date` timestamp NULL DEFAULT NULL, 
    `invoice_only` tinyint(1) DEFAULT NULL, 
    PRIMARY KEY (`id`), 
    KEY `internal` (`internal`) 
) ENGINE=MyISAM AUTO_INCREMENT=43341 DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci; 

任何想法,爲什麼會是什麼?

+0

你是什麼意思「出來作爲BLOB」? – 2010-07-19 10:31:30

+0

@Pekka BLOB數據類型,而不是VARCHAR,基本上這個: http://img826.imageshack.us/img826/248/blob.jpg – cypher 2010-07-19 10:33:17

回答

9

當您使用UNION創建視圖時,必須確保相應列的數據類型相同(或者至少相似,以便將其轉換爲另一個)。在當前情況下,視圖的第一列是publishersprice_vat,除了BLOB之外,沒有任何數據類型定義可以很好地理解這一點。

如果你真的需要這是一個觀點,你可以嘗試...

SELECT e.price_vat AS price_vat, 
     NULL  AS publishers, 
     ...etc... 
     'e'   AS type 
    FROM ama_euromedia_products AS e; 
UNION ALL 
SELECT 
    NULL   AS price_vat, 
    k.publishers AS publishers, 
    ...etc... 
    'k'   AS type 
    FROM ama_kosmas_products AS k; 

得到一個單一的數據類型爲每一列。

+0

好吧,所以基本上問題是,在例子prod_id派生自e。是從k導出的INT(9)和prod_id。是一個VARCHAR(255)? – cypher 2010-07-19 10:35:22

+0

廣告。編輯 - 設置NULL作爲發佈者不會幫助,在視圖的兩個部分中都有完全相同的別名列名 - 這就是爲什麼我將它作爲視圖來完成這一點。如果我理解正確,更改原始表中的字段的數據類型將有所幫助? – cypher 2010-07-19 10:40:19

+2

您可以嘗試'SELECT CAST(e.price_vat AS VARCHAR(255))AS price_vat'(或類似的),而不是更改原始表;這可能會更好。如果它們與第一個查詢中的列名不同,那麼在「UNION」的後半部分中設置列名也沒有意義;該視圖的列只能有一個名稱。 – 2010-07-19 10:45:51