【发布时间】:2015-07-10 18:23:00
【问题描述】:
我正在尝试使用我的数据库中的一组测试数据创建一个随机名称生成器查询。
name 字段存储客户全名,但我希望查询从name 字段中获取随机名字,从name 字段中获取随机姓氏。
查询:
select concat(first_name, ' ', last_name) from
((select lcase(SUBSTRING_INDEX(SUBSTRING_INDEX(name, ' ', 1), ' ', -1)) as first_name
from customers
where SUBSTRING_INDEX(SUBSTRING_INDEX(name, ' ', 1), ' ', -1) not like '%Mrs'
and SUBSTRING_INDEX(SUBSTRING_INDEX(name, ' ', 1), ' ', -1) not like '%Mrs'
and SUBSTRING_INDEX(SUBSTRING_INDEX(name, ' ', 1), ' ', -1) not like '%Mr'
and SUBSTRING_INDEX(SUBSTRING_INDEX(name, ' ', 1), ' ', -1) not like '%.%'
and length(SUBSTRING_INDEX(SUBSTRING_INDEX(name, ' ', 1), ' ', -1)) > 1
order by rand()
limit 10) as first_name_tbl,
(select lcase(SUBSTRING_INDEX(SUBSTRING_INDEX(name, ' ', 2), ' ', -1)) as last_name
from customers
where SUBSTRING_INDEX(SUBSTRING_INDEX(name, ' ', 2), ' ', -1) not like '%Mrs'
and SUBSTRING_INDEX(SUBSTRING_INDEX(name, ' ', 2), ' ', -1) not like '%Mrs'
and SUBSTRING_INDEX(SUBSTRING_INDEX(name, ' ', 2), ' ', -1) not like '%Mr'
and SUBSTRING_INDEX(SUBSTRING_INDEX(name, ' ', 2), ' ', -1) not like '%.%'
and length(SUBSTRING_INDEX(SUBSTRING_INDEX(name, ' ', 2), ' ', -1)) > 1
order by rand()
limit 10) as last_name_tbl);
我的查询的问题是它返回了重复的名称而不是正确的记录数。
当前结果:
100 rows in set
| sabrina mole |
| daniel mole |
| helen mole |
| jenny mole |
| caroline mole |
| catherine mole |
| julia mole |
| carmella mole |
| mark mole |
| catharine mole |
| sabrina salgado |
| daniel salgado |
| helen salgado |
| jenny salgado |
| caroline salgado |
| catherine salgado |
| julia salgado |
| carmella salgado |
.....
期望的结果
10 rows in set
| sabrina mole |
| daniel salgado |
| helen oinn |
| jenny hird |
| caroline thompson |
| catherine helena |
| julia taylor |
| carmella spectrum |
| mark storrie |
| catharine pat |
【问题讨论】:
-
当你说重复的名字时,你的意思是,你只希望每个名字和姓氏都使用一次吗?
-
@HolmesIV 是的,完全正确。