简体   繁体   English

从json数据中获取数据表结构

[英]i nf data table structure from json data

I have a database (mysql) table like 我有一个数据库(mysql)表

id |  party  |  rights    | m_id
---+---------+------------+---------
9  |abc      | 3,5,6      | ["12,","15,"6"]
20 |xyz      |  5,2       | ["6,","2,"9","12"]
21 |xyz 1    |  5,2       | ["6,","9,"12"]

Now I want to make my table in this way search result for rights 5 is ["12,","15,"6"] ["6,","2,"12"] ["6,","9,"12"] 现在我想以这种方式制作我的表格权利5的搜索结果是[“12”,“15,”6“] [”6“,”2,“12”] [“6,”,“9 , “12”]

12 | 12 | abc , xyz,xyz1 | abc,xyz,xyz1 |

15 | 15 | abc| ABC |

6 | 6 | abc , xyz,xyz1 | abc,xyz,xyz1 |

9 | 9 | xyz,xyz1 | xyz,xyz1 |

Let's start with what I believe you already have. 让我们从我相信你已经拥有的东西开始。 This is an sscce . 这是一个sscce If you adjust the mysql credentials it should run on your system, creating only a temporary MySQL table. 如果你调整mysql凭据,它应该在你的系统上运行,只创建一个临时的MySQL表。 It uses PDO to access the MySQL server. 它使用PDO访问MySQL服务器。 Which API you actually use is not important (ie as long as the other API is mysqli, because the mysql_* functions are depreacted ;-)) 实际使用哪个API并不重要(即只要其他API是mysqli,因为mysql_ *函数被解析 ;-))

<?php
$pdo = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'localonly', 'localonly', array(
    PDO::ATTR_EMULATE_PREPARES=>false,
    PDO::MYSQL_ATTR_DIRECT_QUERY=>false,
    PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION
));
setup($pdo);

$statement = $pdo->prepare('
    SELECT
        *
    FROM
        soFoo
    WHERE
        FIND_IN_SET(:right, rights)
');

$statement->execute( array(':right'=>5) );

/* in one way or another you have a loop where you fetch the records
having '5' in the `rights` field
*/
foreach( $statement as $row ) {
    echo $row['party'], ' ', $row['show_ids'], "\r\n";
}


function setup($pdo) {
    $pdo->exec('
        CREATE TEMPORARY TABLE soFoo
    (`id` int, `party` varchar(9), `exc` varchar(13), `rights` varchar(5), `show_ids` varchar(27))
   ');
  $pdo->exec( <<< eos
        INSERT INTO soFoo
            (`id`, `party`, `exc`, `rights`, `show_ids`)
        VALUES
            (9, 'Percept', 'Non-Exclusive', '3,5,6', '["12,","15,"6"]'),
            (20, 'Tata Sky', 'Non-Exclusive', '5,4,', '["6,","9,"11"]'),
            (21, 'Tata Sky', 'Exclusive', '5,4', '["6,","13","15,"2","4","9"]'),
            (22, 'Simbiotic', 'Exclusive', '6,2', '["12,","15,"1","6","7","8"]')
eos
    );
}

this prints 这打印

Percept ["12,","15,"6"]
Tata Sky ["6,","9,"11"]
Tata Sky ["6,","13","15,"2","4","9"]

and is (as I understand the question) as far as you've already got. 就你已经得到的而言(正如我所理解的那样)。

Now let's decode the JSON array and check whether it contains the element 9 . 现在让我们解码JSON数组并检查它是否包含元素9 If it does add inforamtion from the current row to an array called $parties 如果它确实将当前行的信息添加到名为$parties的数组中

$parties = array();
/* in one way or another you have a loop where you fetch the records
having '5' in the `rights` field
*/
foreach( $statement as $row ) {
    $ids = json_decode($row['show_ids'], true);
    if ( in_array('9', $ids) ) {
        $parties[$row['id']] = $row['party'];
    }
}
var_export($parties);

prints 版画

array (
  20 => 'Tata Sky',
  21 => 'Tata Sky',
)

But ... from a relational database point of view this is ....suboptimal. 但是......从关系数据库的角度来看,这是......次优的。
The FIND_IN_SET clause hinders MySQL from using indices effectively; FIND_IN_SET子句阻碍了MySQL有效地使用索引; you're searching (compound) data within a single field. 您正在搜索的单一领域 (化合物)的数据。 It's amazing what the database server implementations can do to improve performance; 令人惊讶的是,数据库服务器实现可以提高性能; but it has limits. 但它有限制。
And you're also transfering possibly unnecessary data from the MySQL server to the php instance (those records that have 5 in rights but not 9 in show_ids ). 而且你也可能将不必要的数据从MySQL服务器传输到php实例(那些在rights中有5而在show_ids没有9show_ids )。 If possible, this should be avoided. 如果可能,应该避免这种情况。 Networks/Network stacks are fast and can be optimized, RAM is cheap ...but again, there are limits. 网络/网络堆栈速度快,可以优化,RAM很便宜......但同样,有限制。

So, I suggest you look into Database normalization on the one hand and/or document-oriented databases on the other hand. 因此,我建议您一方面研究数据库规范化 ,另一方面研究面向文档的数据库

I am going to give a example below so you can write in your way, 我将在下面给出一个例子,这样你就可以按照自己的方式写作,

If you are having table below 如果你有下表

id | name    
1  | a,b,c    
2  | b

and expected output like this 和这样的预期输出

id | name    
1  | a    
1  | b    
1  | c    
2  | b

Then follow below solutions 然后按照以下解决方案

If you can create a numbers table, that contains numbers from 1 to the maximum fields to split, you could use a solution like this: 如果您可以创建一个数字表,其中包含从1到要拆分的最大字段的数字,您可以使用如下解决方案:

select
  tablename.id,
  SUBSTRING_INDEX(SUBSTRING_INDEX(tablename.name, ',', numbers.n), ',', -1) name
from
  numbers inner join tablename
  on CHAR_LENGTH(tablename.name)
     -CHAR_LENGTH(REPLACE(tablename.name, ',', ''))>=numbers.n-1
order by
  id, n

Please see fiddle here. 请看这里的小提琴

If you cannot create a table, then a solution can be this: 如果您无法创建表,那么解决方案可以是:

select
  tablename.id,
  SUBSTRING_INDEX(SUBSTRING_INDEX(tablename.name, ',', numbers.n), ',', -1) name
from
  (select 1 n union all
   select 2 union all select 3 union all
   select 4 union all select 5) numbers INNER JOIN tablename
  on CHAR_LENGTH(tablename.name)
     -CHAR_LENGTH(REPLACE(tablename.name, ',', ''))>=numbers.n-1
order by
  id, n

an example fiddle is here. 这里有一个例子

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

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