在sql查询中分配自定义行值

时间:2012-12-14 13:28:09

标签: mysql

我有多表+不同的字段,我尝试做这样的事情:

SELECT
   `title` as title,
   `manufacturer`+`model` as content /* I want to achieve this as well */
   `type` = 'auto_table' /* custom variable w/ value auto_table to know from where is this data*/
    FROM `auto`
    WHERE `title` LIKE '%bmw%'
    OR
    `manufacturer` LIKE '%bmw'
    OR
    `other_data` LIKE '%bmw'
UNION
    SELECT
       `title` as title,
       `content` as content
       `type` = 'news_page' /* custom variable w/ value news_page to know from where is this data*/
        FROM `news`
        WHERE `title` LIKE '%bmw%'
        OR
        `content` LIKE '%bmw'

我的意思是我想在动态选择期间添加一行及其值。 另一个我想将两个字段合并为manufacturer + model作为title

3 个答案:

答案 0 :(得分:1)

CONCAT (manufacturer, model) as content

答案 1 :(得分:0)

试试这个:

SELECT
   title,
   CONCAT(manufacturer,model) as content
   'auto_table' as type
FROM [...]

答案 2 :(得分:0)

很难得到你真正追求的东西。你想一次性插入选择吗?

INSERT INTO your_table
SELECT
   `title` as title,
   CONCAT(`manufacturer`,' ', `model`) as content /* I want to achieve this as well */
   `type` = 'auto_table' /* custom variable w/ value auto_table to know from where is this data*/
    FROM `auto`
    WHERE `title` LIKE '%bmw%'
    OR
    `manufacturer` LIKE '%bmw'
    OR
    `other_data` LIKE '%bmw'
UNION
    SELECT
       `title` as title,
       `content` as content
       `type` = 'news_page' /* custom variable w/ value news_page to know from where is this data*/
        FROM `news`
        WHERE `title` LIKE '%bmw%'
        OR
        `content` LIKE '%bmw'

使用CONCAT连接值