将MS SQL枢纽查询转换为MySQL枢纽查询

时间:2019-02-09 05:20:53

标签: mysql sql-server

我的MS-SQL数据库中有7个表,这些表已转移到MySQL

protocols,
arms,
visit_groups,
visits,line_items,
line_items_visits,
services

这是其中的数据图:

1)协议表

protocol

2)武器桌

enter image description here

3)visit_groups表

enter image description here

4)访问表

enter image description here

5)line_items表

enter image description here

6)line_items_visits表

enter image description here

7)服务表

enter image description here

我正在MS SQL Server上对此执行枢轴操作,并且得到了所需的结果。这是MS-SQL查询及其结果

DECLARE @query AS NVARCHAR(MAX)
set @query = 'SELECT id, name, serviceName, subject_count,' 
         + '[Visit 1],[Visit 2]' + ' FROM 
         (
            select protocols.id, arms.name, visit_groups.name as visitGroup, services.name as serviceName, 
            visits.quantity, 
            line_items_visits.subject_count
            from protocols,arms,visit_groups,visits,line_items,line_items_visits,services
            where protocols.id = arms.protocol_id
            and visit_groups.arm_id = arms.id
            and visits.visit_group_id = visit_groups.id
            and line_items_visits.arm_id = arms.id
            and visits.line_items_visit_id = line_items_visits.id
            and line_items_visits.line_item_id = line_items.id
            and services.id = line_items.service_id
            and protocols.id = ''' + '591' + '''
            and arms.id = ''' + '377' + '''

        ) x
        pivot 
        (
            max(quantity)
            for visitGroup in (' + '[Visit 1],[Visit 2]' + ')
        ) p';

 execute (@query)

这是我得到的结果

enter image description here

现在我将所有表都转移到mysql,并尝试运行此查询,但未得到任何结果。您能帮我将这种主要的MS SQL操作转换为mysql查询吗?

0 个答案:

没有答案