Home > database >  MySQL query question
MySQL query question

Time:10-02

Business need to query the ads list, there are two kinds of type, respectively in the two tables preservation, need to combine the results of the two tables according to time flashbacks sort, all fields to be displayed, but the two tables there are part of the field is different, if the query result without this field is NULL, the other need to add a field indicates which result is to belong to what type (query from which table),

I met the problem:
First two different table fields (Err) caused by 1222 - The informs The SELECT statements have a company's number of columns
Then don't know how to insert said results source table name field
I want to consult the business only through the SQL statement to achieve this? If you can do to achieve?

Structure is as follows:
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
- Table structure for advert_carousel
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
The CREATE TABLE ` advert_carousel ` (
` advert_carousel_id ` int (11), NOT NULL AUTO_INCREMENT COMMENT 'by advertising logo,
` biz_user_id ` int (11), NOT NULL COMMENT 'corporate identity',
` file_lib_carousel ` int (11), NOT NULL COMMENT 'image identification,
` start_time ` datetime NOT NULL COMMENT 'start time',
` end_time ` datetime NOT NULL COMMENT 'end time,
` create_time ` datetime NOT NULL COMMENT 'creation time,
` modify_time ` datetime DEFAULT NULL COMMENT 'last modified time,
` redirect_url ` varchar (255) the DEFAULT NULL COMMENT 'jump URL,
` industry_id ` int (11) the DEFAULT NULL COMMENT 'scope; Industry identity; Left blank for the home page ',
` status ` varchar (20) NOT NULL DEFAULT 'activation' COMMENT 'status; The activation=activation; Invalid=failure; Does=removed ',
PRIMARY KEY (` advert_carousel_id `)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
- Table structure for advert_recommend
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
The CREATE TABLE ` advert_recommend ` (
` advert_recommend_id ` int (11), NOT NULL AUTO_INCREMENT COMMENT 'identity',
` biz_user_id ` int (11), NOT NULL COMMENT 'corporate identity',
` industry_id ` int (11), NOT NULL COMMENT 'industry logo,
` start_time ` datetime NOT NULL COMMENT 'start time',
` end_time ` datetime NOT NULL COMMENT 'end time,
` create_time ` datetime NOT NULL COMMENT 'creation time,
` modify_time ` datetime DEFAULT NULL COMMENT 'last modified time,
` click_count ` int (11) the DEFAULT '0' COMMENT 'total clicks,
` status ` varchar (20) NOT NULL DEFAULT 'activation' COMMENT 'status; The activation=activation; Invalid=failure; Does=removed ',
PRIMARY KEY (` advert_recommend_id `)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CodePudding user response:

 seledct advert_carousel_id, biz_user_id file_lib_carousel, null as ndustry_id 
The from advert_carouse
Union all
Seledct advert_carousel_id biz_user_id, null as file_lib_carousel, ndustry_id
The from advert_recommend
  • Related