简体   繁体   English

如何从2个不同的表中获取一列

[英]How to get one column from 2 different tables

I have 2 tables in MySQL registerSMSusers and GroupsSMS . 我在MySQL registerSMSusersGroupsSMS中有2个表。 Both the tables have a column named as mobile . 这两个表都有一个名为mobile的列。 From an HTML form I am getting comma separated values like test,alltest,john . 从HTML表单我得到逗号分隔值,如test,alltest,john These comma separated values will be present in either of the 2 tables. 这些逗号分隔值将出现在2个表中的任何一个中。 For example test (name column) is present in registerSMSusers and alltest is present in GroupsSMS (GroupName column). 例如, test (name列)存在于registerSMSusers中alltest存在于GroupsSMS (GroupName列)中。

In Java I can split with comma and then check if its present in any of the tables or not.If present then get the mobile. 在Java中,我可以用逗号分割,然后检查它是否存在于任何表中。如果存在,则获取移动设备。 Just wanted to know are there any SQL queries for the same. 只是想知道是否有相同的SQL查询。

This is SQL schema 这是SQL架构

DROP TABLE IF EXISTS `GroupsSMS`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `GroupsSMS` (
  `Name` varchar(50) DEFAULT NULL,
  `email` varchar(50) DEFAULT NULL,
  `mobile` varchar(20) DEFAULT NULL,
  `GroupName` varchar(20) DEFAULT NULL,
  `GroupID` int(11) NOT NULL AUTO_INCREMENT,
  `dataselected` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`GroupID`)
) ENGINE=MyISAM AUTO_INCREMENT=191 DEFAULT CHARSET=latin1;

DROP TABLE IF EXISTS `registerSmsUsers`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `registerSmsUsers` (
  `name` varchar(50) DEFAULT NULL,
  `email` varchar(50) DEFAULT NULL,
  `mobile` varchar(20) DEFAULT NULL,
  `uid` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`uid`),
  UNIQUE KEY `mobile` (`mobile`),
  UNIQUE KEY `email` (`email`)
) ENGINE=MyISAM AUTO_INCREMENT=83 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;

And this is the sqlfiddle 这就是sqlfiddle

MySQL does not have a ready made function for splitting a CSV string. MySQL没有用于拆分CSV字符串的现成功能。 You have to do it manually using SUBSTRING using SUBSTRING_INDEX or using a REGEXP . 您必须使用SUBSTRING使用SUBSTRING_INDEX或使用REGEXP手动执行此操作。 See details on a similar problem here 这里查看类似问题的详细信息

After you have say split the CSV into actual strings which are stored in a table 'CSVTable' {id, strvalue}, you can check like 在您说将CSV拆分为存储在表'CSVTable'{id,strvalue}中的实际字符串后,您可以检查

SELECT G.mobile as mobilenumber 
FROM 'GroupsSMS' G LEFT JOIN 'CSVTable' C 
on G.GroupName =C.strvalue 
WHERE C.strvalue is NOT NULL

UNION 

SELECT R.mobile as mobilenumber 
FROM 'registerSMSusers' R LEFT JOIN 'CSVTable' C 
on R.name=C.strvalue 
WHERE C.strvalue is NOT NULL

Note I have not used UNION ALL to get distinct set values 注意我没有使用UNION ALL来获取不同的设置值

Pseudo code for getting values into temp table 用于将值获取到临时表的伪代码

DECLARE @CSVTABLE TABLE ( id int not null, strvalue NVARCHAR(400) NOT NULL)
DECLARE @var int
SET @var=1
DECLARE @STREXP NVARCHAR(MAX)
DECLARE @BUFF NVARHCAR(400)
SET @BUFF=SUBSTRING_INDEX(@STREXP,',',1)
SET @STREXP=REPLACE(@STREXp,@BUFF+',','')
    WHILE @BUFF IS NOT NULL DO
    INSERT INTO @temp VALUES(@var,@BUFF)
    @var=@var+1
    @VUFF
    END WHILE

I you have split the string in Java 我在Java中拆分了字符串

String names[] = csv.split(',');

You can search for the corresponding mobile number in either registerSmsUsers or GroupsSMS with 您可以在registerSmsUsersGroupsSMS搜索相应的手机号码

PreparedStatement stmt = conn.prepareStatment("select u.mobile from registerSmsuser u where u.name = ? union select g.mobile from GroupsSMS g where g.groupname = ?");
stmt.setString(1, names[0]);
stmt.setString(2, names[0]);
ResultSet rs = stmt.executeQuery();
if (rs.first()) {
    // do something with the mobile number
}

This will select entries from both the user and the groups table. 这将从用户和组表中选择条目。 If you need to know, where the number is from, you can add a fixed string to your select 如果您需要知道号码的来源,您可以在选择中添加固定字符串

select u.mobile, 'user' as origin from registerSmsuser u ...
union
select g.mobile, 'groups' as origin from GroupsSMS g ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM