我是PHP和MySQL连接的初学者,但我真的很愿意解决这个问题,我已经花了一些时间在它上面。我对我制作的数据库没有任何问题,但是这个数据库是来自我的一个客户端的外部数据库,他想从DB queires构建一个HTML表。数据库中的数据完全混合并添加到同一列中,这就是为什么理解SQL查询语法对我来说很复杂。
在数据库中,我有表中的所有数据:trr_postmeta
meta_id post_id meta_key meta_value
3515 1479 company_sidlo Praha
3516 1479 company_ico 02925885
3517 1479 company_date 04/23/2014
3518 1479 company_price 29400
3519 1479 company_url https://or.justice.cz/ias/ui/rejstrik-firma.vysled...
3520 1479 company_state zamluvená
3530 1481 _edit_last 7
3531 1481 _edit_lock 1413898849:7
3532 1481 company_typ s.r.o.
3533 1481 company_sidlo Praha
3534 1481 company_ico 02925605
3535 1481 company_date 04/23/2014
3536 1481 company_price 26900
对于我的HTML输出,我需要这样的东西:
--------------------
ID | ICO | Typ
--------------------
1 | 64654664 | sro
--------------------
我的SQL查询如下:
SELECT meta_id, meta_value FROM trr_postmeta WHERE meta_key = "company_typ" AND meta_key = "company_ico" AND meta_key = "company_date" AND meta_key = "company_price" AND meta_key = "company_url" AND meta_key = "company_state";
但我认为我的语法WHERE仅适用于第一个条件。你能帮我解决我做错的事吗?我认为99%是SQL查询语法,但我无法弄清楚这一点。我需要在一列中获得company_typ和company_ico等的值。
由于
答案 0 :(得分:0)
如果我理解你想要将行值作为列标题,我创建了一个小例子,你可以根据你的要求修改它
mysql> create table trr_postmeta (meta_key varchar(100),meta_value varchar(100));
Query OK, 0 rows affected (0.11 sec)
mysql> insert into trr_postmeta values ('company_typ','ABC'),('company_ico','pic.jpg'),('company_date','2014-11-28'),('company_state','LA');
Query OK, 4 rows affected (0.05 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> select * from trr_postmeta;
+---------------+------------+
| meta_key | meta_value |
+---------------+------------+
| company_typ | ABC |
| company_ico | pic.jpg |
| company_date | 2014-11-28 |
| company_state | LA |
+---------------+------------+
4 rows in set (0.00 sec)
所以查询看起来像
select
group_concat(if(meta_key = 'company_typ',meta_value,null)) as 'Company Type',
group_concat(if(meta_key = 'company_ico',meta_value,null)) as 'Company Icon',
group_concat(if(meta_key = 'company_state',meta_value,null)) as 'Company State'
from trr_postmeta ;
输出看起来像
+--------------+--------------+---------------+
| Company Type | Company Icon | Company State |
+--------------+--------------+---------------+
| ABC | pic.jpg | LA |
+--------------+--------------+---------------+
答案 1 :(得分:0)
一定是一年中的时间......
DROP TABLE IF EXISTS eav_hell;
CREATE TABLE eav_hell
(meta_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,post_id INT NOT NULL
,meta_key VARCHAR(20) NOT NULL
,meta_value VARCHAR(100) NOT NULL
,UNIQUE(post_id,meta_key)
);
INSERT INTO eav_hell VALUES
(3515 ,1479 ,'company_sidlo','Praha'),
(3516 ,1479 ,'company_ico','02925885'),
(3517 ,1479 ,'company_date','04/23/2014'),
(3518 ,1479 ,'company_price','29400'),
(3519 ,1479 ,'company_url','https://or.justice.cz/ias/ui/rejstrik-firma.vysled...'),
(3520 ,1479 ,'company_state','zamluvená'),
(3530 ,1481 ,'_edit_last','7'),
(3531 ,1481 ,'_edit_lock','1413898849:7'),
(3532 ,1481 ,'company_typ','s.r.o.'),
(3533 ,1481 ,'company_sidlo','Praha'),
(3534 ,1481 ,'company_ico','02925605'),
(3535 ,1481 ,'company_date','04/23/2014'),
(3536 ,1481 ,'company_price','26900');
SELECT post_id
, MAX(CASE WHEN meta_key = 'company_typ' THEN meta_value END) company_typ
, MAX(CASE WHEN meta_key = 'company_sidlo' THEN meta_value END) company_sidlo
, MAX(CASE WHEN meta_key = 'company_ico' THEN meta_value END) company_ico
, MAX(CASE WHEN meta_key = 'company_date' THEN meta_value END) company_date
, MAX(CASE WHEN meta_key = 'company_price' THEN meta_value END) company_price
, MAX(CASE WHEN meta_key = 'company_url' THEN meta_value END) company_url
, MAX(CASE WHEN meta_key = 'company_state' THEN meta_value END) company_state
, MAX(CASE WHEN meta_key = '_edit_last' THEN meta_value END) _edit_last
, MAX(CASE WHEN meta_key = '_edit_lock' THEN meta_value END) _edit_lock
FROM eav_hell
GROUP
BY post_id;
+---------+-------------+---------------+-------------+--------------+---------------+-------------------------------------------------------+---------------+------------+--------------+
| post_id | company_typ | company_sidlo | company_ico | company_date | company_price | company_url | company_state | _edit_last | _edit_lock |
+---------+-------------+---------------+-------------+--------------+---------------+-------------------------------------------------------+---------------+------------+--------------+
| 1479 | NULL | Praha | 02925885 | 04/23/2014 | 29400 | https://or.justice.cz/ias/ui/rejstrik-firma.vysled... | zamluvená | NULL | NULL |
| 1481 | s.r.o. | Praha | 02925605 | 04/23/2014 | 26900 | NULL | NULL | 7 | 1413898849:7 |
+---------+-------------+---------------+-------------+--------------+---------------+-------------------------------------------------------+---------------+------------+--------------+
顺便说一句,如果使用像这样的EAV模型,那么根据数据类型将值分成不同的表(或至少列)仍然是一个好主意 - 所以日期在日期表中,数字在数字表,字符串到字符串表。