【问题标题】:Merge two sub-queries row-to-row逐行合并两个子查询
【发布时间】: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 是的,完全正确。

标签: mysql sql


【解决方案1】:

问题是您正在创建一个包含两个 10 行表的交叉连接。

所以 10 x 10 = 100 行。

您需要对每个表使用会话变量 rowid
See rowid on MySql

( SELECT @rowidFirst:=@rowidFirst+1 as rowid, first_name_tbl.*
  FROM  
    ( SELECT .... ) as first_name_tbl
) as firstWithRowID

( SELECT @rowidLast:=@rowidLast+1 as rowid, last_name_tbl.*
  FROM  
    ( SELECT .... ) as last_name_tbl
) as lastWithRowID

然后通过 row_id 加入

SELECT *
FROM firstWithRowID, lastWithRowID
WHERE firstWithRowID.rowid = lastWithRowID.rowid

【讨论】:

  • 我之前尝试过类似的方法,问题是如果你使用order by rand() 我这样做是因为我需要返回随机名称,而不是 rowid 的顺序不正确
  • 这个编辑怎么样,创建随机表后添加row_id?
  • 这是个好主意,我会试一试,然后将结果回复您
猜你喜欢
  • 1970-01-01
  • 2017-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-29
  • 2016-05-26
  • 2012-09-02
相关资源
最近更新 更多