咨询子查询和使用正则表达式

时间:2016-03-16 17:23:21

标签: mysql

我正在尝试识别可以接收药物的用户产生互动。为此,我使用了这段代码:

<configuration>
<property>
<name>dfs.replication</name>
<value>3</value>
</property>
<property>
<name>dfs.namenode.name.dir</name>
<value>file:/home/username/hadoop/yarn/hdfs/namenode</value>
</property>
<property>
<name>dfs.datanode.data.dir</name>
<value>file:/home/username/hadoop/yarn/hdfs/datanode</value>
</property>
</configuration>

我正在本地工作,咨询需要花费太多时间并且下一个错误:错误代码:2013。在查询期间丢失了对MYSQL服务器的连接。可以优化我的代码以避免错误吗?

2 个答案:

答案 0 :(得分:0)

您可以尝试使用in代替regexp。 可能这是更高效的

SELECT c.user, COUNT(DISTINCT c.user) 
FROM mytable AS c
JOIN (SELECT id_beneficiario, mes
    FROM mytable
    WHERE codigo_atc IN  ('C01BD01','N06AA09','J01FA10','J01MA02','J01FA09','N05AH02','L01XE06','R06AA02','A03FA03','
    L04AA27','J02AC01','N06AB03','C03CA01','N05AD01','C03AA03','J02AB02','J01MA12','N05AN01','J01XD01','J05AE04','L01XE08','
    N05AH03','N05AH04','N05AX08','J05AE03','J05AE01','C07AA07','L04AD02','M03BX02','N06AX05','J01EE03') AS d
ON c.user = d.user AND c.mes = d.mes
WHERE c.codigo_atc IN  ('C01BD01','N06AA09','J01FA10','J01MA02','J01FA09','N05AH02','L01XE06','R06AA02','A03FA03','
    L04AA27','J02AC01','N06AB03','C03CA01','N05AD01','C03AA03','J02AB02','J01MA12','N05AN01','J01XD01','J05AE04','L01XE08','
    N05AH03','N05AH04','N05AX08','J05AE03','J05AE01','C07AA07','L04AD02','M03BX02','N06AX05','J01EE03')
GROUP BY c.user;

但是如果你解释了你的相关表模式以及你想要获得什么,可以写一个更简单的查询

答案 1 :(得分:0)

这对你有用吗?

WHERE codigo_atc IN ('C01BD01','N06AA09','J01FA10','J01MA02','J01FA09',
                     'N05AH02','L01XE06','R06AA02','A03FA03','L04AA27',
                     'J02AC01','N06AB03','C03CA01','N05AD01', etc)

如果INcodigo_atc中的任何值匹配,则此(list)子句将返回true。

SQL在设置逻辑方面要比在正则表达式匹配方面好得多。