mysql多个不同表不同结构合并查询,并集查询多个不同表结构数据,部分表结构相同,部分表结构不同的查询方式union All的实现

需求背景

商品表分为三个:基础商品表【aidong_goods】、一存多品商品表【aidong_goods_unit_sku】、一品多存商品表【aidong_goods_specs_sku】

基础商品表和其他两张表有关联id,并三张表部分字段不同,部分字段相同,需要根据条件过滤聚合查询出三张表的商品数据并合并!

实现方式

1.通过查询主商品后再查询其他关联商品

缺点是不方便分页查询,还需要循环,速度和性能都可能受到较大影响

2.关联查询

缺点是无法直接并集拿到数据

3.通过union all实现联合查询

union会过滤数据,所以采用union all

其他方式暂时未考虑采用,比如子查询、临时表、视图查询

具体实现

通过union all实现联合查询,并结合join实现复杂的条件查询。以及自定义字段的命名解决不同表结构问题,使用子查询包含后可以实现整体分页和条件查询

SELECT
	t.*
FROM
	(
		SELECT
			aidong_goods.goods_id,
			aidong_goods.tenant_id,
			aidong_goods.shop_id,
			aidong_goods.goods_sn,
			aidong_goods.goods_name,
			aidong_goods.goods_img,
			aidong_goods.brand_id,
			aidong_goods.market_price,
			aidong_goods.selling_price,
			aidong_goods.selling_stock,
			aidong_goods.limit_selling_stock,
			aidong_goods.storage_room_stock,
			aidong_goods.invented_sale_count,
			aidong_goods.reality_sale_count,
			aidong_goods.unit_id,
			aidong_goods.goods_describe,
			aidong_goods.is_shelves,
			aidong_goods.sys_cat_id1,
			aidong_goods.sys_cat_id2,
			aidong_goods.sys_cat_id3,
			aidong_goods.shop_cat_id1,
			aidong_goods.shop_cat_id2,
			aidong_goods.data_flag,
			aidong_goods.create_time,
			aidong_goods.mark_icon,
			aidong_goods.is_standard_commodity,
			aidong_goods.intelligence_remark,
			aidong_goods.shop_goods_sort,
			aidong_goods.goods_distribution,
			aidong_goods.member_price,
			aidong_goods.integral_reward,
			aidong_goods.goods_type,
			aidong_goods.stock_warning_num,
			aidong_goods.integral_rate,
			aidong_goods.buy_num_limit,
			aidong_goods.min_buy_num,
			aidong_goods.is_becyclebin,
			aidong_goods.limit_daily_day,
			aidong_goods.DELAYED,
			aidong_goods.plu_code,
			aidong_goods.vedio_url,
			aidong_goods.py_initials,
			aidong_goods.international_bar_code,
			aidong_goods.recent_purchase_price,
			aidong_goods.purchase_type,
			aidong_goods.supplier_id_or_buyer_id,
			aidong_goods.quality_guarantee_period,
			aidong_goods.goods_alias,
			aidong_goods.is_sale,
			aidong_goods.weight,
			aidong_goods.volume,
			aidong_goods.to_be_weighed_type,
			aidong_goods.individual_enterprise_main_show,
			aidong_goods.individual_enterprise_limit_purchase_show,
			aidong_goods.producer,
			aidong_goods.processing_mode_status,
			aidong_goods.is_free_shipping,
			aidong_goods.delivery_freight_template_id,
			aidong_goods.inventory_deduction_type,
			aidong_goods.is_enable_batch,
			aidong_goods.room_stock_avg_price,
			aidong_goods.loss_rate,
			0 AS sku_id,
			0 AS unit_transform_num
		FROM
			aidong_goods
		UNION ALL
			SELECT
				aidong_goods_unit_sku.goods_id,
				aidong_goods_unit_sku.tenant_id,
				aidong_goods_unit_sku.shop_id,
				aidong_goods_unit_sku.goods_sn,
				aidong_goods.goods_name,
				aidong_goods.goods_img,
				aidong_goods.brand_id,
				aidong_goods_unit_sku.market_price,
				aidong_goods_unit_sku.selling_price,
				aidong_goods_unit_sku.selling_stock,
				aidong_goods_unit_sku.limit_selling_stock,
				aidong_goods.storage_room_stock,
				aidong_goods.invented_sale_count,
				aidong_goods.reality_sale_count,
				aidong_goods_unit_sku.unit_id,
				aidong_goods_unit_sku.goods_describe,
				aidong_goods.is_shelves,
				aidong_goods.sys_cat_id1,
				aidong_goods.sys_cat_id2,
				aidong_goods.sys_cat_id3,
				aidong_goods.shop_cat_id1,
				aidong_goods.shop_cat_id2,
				aidong_goods_unit_sku.data_flag,
				aidong_goods.create_time,
				aidong_goods.mark_icon,
				aidong_goods_unit_sku.is_standard_commodity,
				aidong_goods.intelligence_remark,
				aidong_goods.shop_goods_sort,
				aidong_goods.goods_distribution,
				aidong_goods_unit_sku.member_price,
				aidong_goods.integral_reward,
				aidong_goods.goods_type,
				aidong_goods.stock_warning_num,
				aidong_goods_unit_sku.integral_rate,
				aidong_goods_unit_sku.buy_num_limit,
				aidong_goods_unit_sku.min_buy_num,
				aidong_goods.is_becyclebin,
				aidong_goods_unit_sku.limit_daily_day,
				aidong_goods.DELAYED,
				aidong_goods_unit_sku.plu_code,
				aidong_goods.vedio_url,
				aidong_goods.py_initials,
				aidong_goods_unit_sku.international_bar_code,
				aidong_goods.recent_purchase_price,
				aidong_goods.purchase_type,
				aidong_goods.supplier_id_or_buyer_id,
				aidong_goods.quality_guarantee_period,
				aidong_goods.goods_alias,
				aidong_goods_unit_sku.is_sale,
				aidong_goods_unit_sku.weight,
				aidong_goods_unit_sku.volume,
				aidong_goods_unit_sku.to_be_weighed_type,
				aidong_goods_unit_sku.individual_enterprise_main_show,
				aidong_goods_unit_sku.individual_enterprise_limit_purchase_show,
				aidong_goods.producer,
				aidong_goods.processing_mode_status,
				aidong_goods.is_free_shipping,
				aidong_goods.delivery_freight_template_id,
				aidong_goods.inventory_deduction_type,
				aidong_goods.is_enable_batch,
				aidong_goods.room_stock_avg_price,
				aidong_goods.loss_rate,
				aidong_goods_unit_sku.sku_id,
				aidong_goods_unit_sku.unit_transform_num
			FROM
				aidong_goods
			RIGHT JOIN aidong_goods_unit_sku ON aidong_goods.goods_id = aidong_goods_unit_sku.goods_id
			UNION ALL
				SELECT
					aidong_goods_specs_sku.goods_id,
					aidong_goods_specs_sku.tenant_id,
					aidong_goods_specs_sku.shop_id,
					aidong_goods_specs_sku.goods_sn,
					aidong_goods.goods_name,
					aidong_goods_specs_sku.goods_img,
					aidong_goods.brand_id,
					aidong_goods_specs_sku.market_price,
					aidong_goods_specs_sku.selling_price,
					aidong_goods_specs_sku.selling_stock,
					aidong_goods_specs_sku.limit_selling_stock,
					aidong_goods_specs_sku.storage_room_stock,
					aidong_goods_specs_sku.invented_sale_count,
					aidong_goods_specs_sku.reality_sale_count,
					aidong_goods_specs_sku.unit_id,
					aidong_goods_specs_sku.goods_describe,
					aidong_goods.is_shelves,
					aidong_goods.sys_cat_id1,
					aidong_goods.sys_cat_id2,
					aidong_goods.sys_cat_id3,
					aidong_goods.shop_cat_id1,
					aidong_goods.shop_cat_id2,
					aidong_goods_specs_sku.data_flag,
					aidong_goods.create_time,
					aidong_goods.mark_icon,
					aidong_goods_specs_sku.is_standard_commodity,
					aidong_goods.intelligence_remark,
					aidong_goods.shop_goods_sort,
					aidong_goods.goods_distribution,
					aidong_goods_specs_sku.member_price,
					aidong_goods.integral_reward,
					aidong_goods.goods_type,
					aidong_goods_specs_sku.stock_warning_num,
					aidong_goods_specs_sku.integral_rate,
					aidong_goods_specs_sku.buy_num_limit,
					aidong_goods_specs_sku.min_buy_num,
					aidong_goods.is_becyclebin,
					aidong_goods_specs_sku.limit_daily_day,
					aidong_goods_specs_sku.delayed,
					aidong_goods_specs_sku.plu_code,
					aidong_goods.vedio_url,
					aidong_goods.py_initials,
					aidong_goods_specs_sku.international_bar_code,
					aidong_goods_specs_sku.recent_purchase_price,
					aidong_goods.purchase_type,
					aidong_goods.supplier_id_or_buyer_id,
					aidong_goods.quality_guarantee_period,
					aidong_goods.goods_alias,
					aidong_goods_specs_sku.is_sale,
					aidong_goods_specs_sku.weight,
					aidong_goods_specs_sku.volume,
					aidong_goods_specs_sku.to_be_weighed_type,
					aidong_goods_specs_sku.individual_enterprise_main_show,
					aidong_goods_specs_sku.individual_enterprise_limit_purchase_show,
					aidong_goods.producer,
					aidong_goods.processing_mode_status,
					aidong_goods.is_free_shipping,
					aidong_goods.delivery_freight_template_id,
					aidong_goods.inventory_deduction_type,
					aidong_goods.is_enable_batch,
					aidong_goods_specs_sku.room_stock_avg_price,
					aidong_goods.loss_rate,
					aidong_goods_specs_sku.sku_id,
					0 AS unit_transform_num
				FROM
					aidong_goods
				RIGHT JOIN aidong_goods_specs_sku ON aidong_goods.goods_id = aidong_goods_specs_sku.goods_id
	) t

where goods_name="西瓜"

limit 1,10

 注意事项

自定义字段时 比如数字的可以给  0 as xxx  字符串 '' as xxx

字段顺序不能出错 否则聚合出来的数据字段不正确

如果是强类型语言可以自己对比处理,例如自动生成自定义字段以及字段类型和别名。以上的sql就是在go环境下通过获取表模型自动生成的,不然手工定义大量字段还要做好一一对应实属不方便

运行结果

 

你可能感兴趣的:(后端,数据库,mysql,数据库,database,后端,golang)