通过在MySQL中传递名称来获取域名

时间:2018-04-27 04:33:12

标签: mysql

我想通过传递名称来获取域名。

考虑以下,

CREATE TABLE `mails` (
  `idmails` int(11) NOT NULL,
  `mails` varchar(45) DEFAULT NULL
);
INSERT INTO mails
VALUES(1,'harishsng@gmail.com'),
(2,'harish.sn@m-tutor.com'),(3,'harishsn@yahoo.in');

当我通过时 案例1:harishsng,结果应该是gmail, 案例2:harish.sn它应该是m-tutor。

我怎样才能在MySQL中做到这一点?

4 个答案:

答案 0 :(得分:4)

SUBSTRING_INDEX在这里派上用场了:

SELECT
    idmails,
    mails,
    SUBSTRING_INDEX(SUBSTRING_INDEX(mails, '@', -1), '.', 1) AS domain
FROM mails;

enter image description here

Demo

答案 1 :(得分:2)

我认为这就是你要找的东西。您可以使用substring_index

select substring_index(substring_index(mails,'.com',1), '@', -1 )  from mails where email like 'harishsng%'

答案 2 :(得分:0)

SELECT替换(替换(替换(邮件,' harishsng'''),' @','') ,' .com','')来自bullet.mails,其邮件类似于'%harishsng%';

答案 3 :(得分:0)

#include <stdio.h>

void foo(char* qux) { puts(qux); }
void bar(char** qux) { puts(*qux); }
void baz(char* qux[12]) { puts(*qux); }

int main() {
    char str[12] = "Hello there";

    foo(str);
    bar(&str); // Compiler warning
    baz(&str); // Same compiler warning

    return 0;
}
相关问题