简体   繁体   English

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

[英]how can i get a temp table result from a string?

for example: 例如:
my products best sort string is "'8207,17631,16717,18545,9062,17469,17246,17750" 我的产品最好排序字符串是''8207,17631,16717,18545,9062,17469,17246,17750“

this string is posted from php , I dont want to store them in datebase. 这个字符串是从php发布的,我不想将它们存储在datebase中。 I want to query datas from mysql and left join a temp table then sort by the temp table'sort. 我想从mysql查询数据并离开加入临时表然后按临时表的排序。

how can I get this temp table from a string ? 如何从字符串中获取此临时表?

在此输入图像描述

my codes seems will be like that bellow:(wrong codes) 我的代码似乎就像下面那样:(错误的代码)

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

Your best approach could be - to use UNION for generating row set from string. 您最好的方法可能是 - 使用UNION从字符串生成行集。 This, however, will require joining your string in your application, like this: 但是,这需要在您的应用程序中加入您的字符串,如下所示:

$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)));

-end result will be like: - 结果将如下:

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

However, if you want to do that in SQL - that will not be easy, since MySQL doesn't supports sequences - and, therefore, you'll need to use some tricks to produce desired rows set. 但是,如果你想在SQL中这样做 - 这并不容易,因为MySQL不支持序列 - 因此,你需要使用一些技巧来产生所需的行集。 There's a way to generate N consecutive numbers with: 有一种方法可以生成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

-this will result in 10 numbers 1..10 (check this fiddle). - 这将导致10数字1..10 (检查这个小提琴)。 Using this, you can get your end result: 使用此功能,您可以获得最终结果:

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

-check this fiddle. - 检查这个小提琴。 However, this may be slow and I recommend you to use your application layer to build desired SQL. 但是,这可能很慢,我建议您使用应用程序层来构建所需的SQL。

something like this? 这样的事情? use UNION to generate inline view. 使用UNION生成内联视图。 this can be generated by Client side. 这可以由客户端生成。

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

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

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