【问题标题】:how can i get a temp table result from a string?如何从字符串中获取临时表结果?
【发布时间】:2013-12-23 18:09:32
【问题描述】:

例如:
我的产品最佳排序字符串是“'8207,17631,16717,18545,9062,17469,17246,17750”

此字符串是从 php 发布的,我不想将它们存储在数据库中。我想从 mysql 中查询数据并左加入一个临时表,然后按临时表的排序进行排序。

如何从字符串中获取此临时表?

我的代码似乎会像下面这样:(错误代码)

select
    p.products_id
from
    (
        select '18207,17631,16717,18545,9062,17469,17246,17750' as products_id
    ) as p
order by p.sort

【问题讨论】:

  • 参见 IN() 或 FIND_IN_SET()

标签: php mysql temp-tables


【解决方案1】:

您最好的方法可能是 - 使用 UNION 从字符串生成行集。但是,这需要在您的应用程序中加入您的字符串,如下所示:

$string = '18207,17631,16717,18545,9062,17469,17246,17750';
$id     = 0;
$sql    = join(' UNION ALL '.PHP_EOL, array_map(function($item) use (&$id)
{
   return 'SELECT '.(++$id).' AS sort, "'.$item.'" AS products_id';
}, explode(',', $string)));

-最终结果将是:

SELECT 1 AS sort, "18207" AS products_id UNION ALL 
SELECT 2 AS sort, "17631" AS products_id UNION ALL 
SELECT 3 AS sort, "16717" AS products_id UNION ALL 
SELECT 4 AS sort, "18545" AS products_id UNION ALL 
SELECT 5 AS sort, "9062" AS products_id UNION ALL 
SELECT 6 AS sort, "17469" AS products_id UNION ALL 
SELECT 7 AS sort, "17246" AS products_id UNION ALL 
SELECT 8 AS sort, "17750" AS products_id

但是,如果您想在 SQL 中执行此操作 - 这并不容易,因为 MySQL 不支持序列 - 因此,您需要使用一些技巧来生成所需的行集。有一种方法可以生成N 连续数字:

SELECT id+1
FROM 
  (SELECT
    (two_1.id + two_2.id + two_4.id + 
    two_8.id + two_16.id) AS id
   FROM
    (SELECT 0 AS id UNION ALL SELECT 1 AS id) AS two_1
    CROSS JOIN (SELECT 0 id UNION ALL SELECT 2 id) AS two_2
    CROSS JOIN (SELECT 0 id UNION ALL SELECT 4 id) AS two_4
    CROSS JOIN (SELECT 0 id UNION ALL SELECT 8 id) AS two_8
    CROSS JOIN (SELECT 0 id UNION ALL SELECT 16 id) AS two_16
   ) AS init
LIMIT 10

-这将导致10 数字1..10(检查this 小提琴)。使用它,您可以获得最终结果:

SELECT 
  ELT(id+1, 18207,17631,16717,18545,9062,17469,17246,17750) AS products_id,
  id+1 AS sort
FROM 
  (SELECT
    (two_1.id + two_2.id + two_4.id + 
    two_8.id + two_16.id) AS id
   FROM
    (SELECT 0 AS id UNION ALL SELECT 1 AS id) AS two_1
    CROSS JOIN (SELECT 0 id UNION ALL SELECT 2 id) AS two_2
    CROSS JOIN (SELECT 0 id UNION ALL SELECT 4 id) AS two_4
    CROSS JOIN (SELECT 0 id UNION ALL SELECT 8 id) AS two_8
    CROSS JOIN (SELECT 0 id UNION ALL SELECT 16 id) AS two_16
   ) AS init
HAVING 
  products_id IS NOT NULL

-检查this 小提琴。但是,这可能会很慢,我建议您使用应用程序层来构建所需的 SQL。

【讨论】:

  • 我唯一的疑问是,通常任何此类问题都是应用 Jaywalker SQL 反模式的结果..
【解决方案2】:

像这样?使用 UNION 生成内联视图。这可以由客户端生成。

SELECT *
FROM
(
    SELECT '18207' AS products_id, 1 as sort
    UNION
    SELECT '17631' AS products_id, 2 as sort
    UNION
    SELECT '16717' AS products_id, 3 as sort
    UNION
    SELECT '18545' AS products_id, 4 as sort
    UNION
    SELECT '9062' AS products_id, 5 as sort
) x JOIN tbl x.products_id = tbl.products_id
ORDER BY sort

【讨论】:

  • 我想把字符串分解成一个表格,当你从我的字符串中使用“select '18207'”时,我怎样才能得到'18207'?
  • @mingfish_004 我怎样才能得到'18207' 我想@Alma 已经回答了这个问题,我有一个问题。你有什么理由不使用TEMPORARY TABLE? MySQL 临时表仅在每个会话中可见,并在会话关闭时自动删除。
  • @mingfish_004 如果字符串有超过+10k products_id,那么我认为使用临时表会更好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多