即使是我自己也在解释这个问题,所以我创建了一个图像,因为它更容易理解。
表格结构:
如果红色字段为空且绿色字段为真,我想从蓝色字段中选择数据。我该怎么做呢? 我已经和它搏斗了很长一段时间了。
无法更改表结构,因为它是从wordpress contactform7到数据库插件。
我非常感谢任何帮助:)
只选择一封电子邮件就可以了,
SELECT field_value FROM wp_cf7dbplugin_submits WHERE field_name = 'your-email'
现在我只想选择电子邮件,如果field_value =''WHERE field_name ='has-wp-user'..
所以我想要的就是这样;
SELECT field_value FROM wp_cf7dbplugin_submits WHERE field_name = 'your-email' IF has-wp-user = 'false'
的Ish。
这种方式有可能吗?
表格结构:
CREATE TABLE IF NOT EXISTS `wp_cf7dbplugin_submits` (
`submit_time` decimal(16,4) NOT NULL,
`form_name` varchar(127) CHARACTER SET utf8 DEFAULT NULL,
`field_name` varchar(127) CHARACTER SET utf8 DEFAULT NULL,
`field_value` longtext CHARACTER SET utf8,
`field_order` int(11) DEFAULT NULL,
`file` longblob,
KEY `submit_time_idx` (`submit_time`),
KEY `form_name_idx` (`form_name`),
KEY `field_name_idx` (`field_name`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumpning av Data i tabell `wp_cf7dbplugin_submits`
--
INSERT INTO `wp_cf7dbplugin_submits` (`submit_time`, `form_name`, `field_name`, `field_value`, `field_order`, `file`) VALUES
('1387497601.8300', 'Bli medlem', 'has-wp-user', '', 0, NULL),
('1387497601.8300', 'Bli medlem', 'paid-membership-fee', 'true', 1, NULL),
('1387497601.8300', 'Bli medlem', 'your-name', 'Firstname Lastname ', 2, NULL),
('1387497601.8300', 'Bli medlem', 'your-email', 'email@mail.se', 3, NULL),
('1387497601.8300', 'Bli medlem', 'your-adress', 'theroad 12', 4, NULL),
('1387497601.8300', 'Bli medlem', 'your-zip', '76045', 5, NULL),
('1387497601.8300', 'Bli medlem', 'your-postal-area', 'postalarea', 6, NULL),
('1387497601.8300', 'Bli medlem', 'your-phonenumber', '0123456789', 7, NULL),
('1387497602.8300', 'Bli medlem', 'has-wp-user', '', 0, NULL),
('1387497602.8300', 'Bli medlem', 'paid-membership-fee', 'true', 1, NULL),
('1387497602.8300', 'Bli medlem', 'your-name', 'Firstname lastname', 2, NULL),
('1387497602.8300', 'Bli medlem', 'your-email', 'email@mail.se', 3, NULL),
('1387497602.8300', 'Bli medlem', 'your-adress', 'singövägen 156', 4, NULL),
('1387497602.8300', 'Bli medlem', 'your-zip', '76045', 5, NULL),
('1387497602.8300', 'Bli medlem', 'your-postal-area', 'grisslehamn', 6, NULL),
('1387497602.8300', 'Bli medlem', 'your-phonenumber', '0123456789', 7, NULL),
('1387584003.8300', 'Bli medlem', 'has-wp-user', '', 0, NULL),
('1387584003.8300', 'Bli medlem', 'paid-membership-fee', 'true', 1, NULL),
('1387584003.8300', 'Bli medlem', 'your-name', 'Firstname Lastname', 2, NULL),
('1387584003.8300', 'Bli medlem', 'your-email', 'email@mail.se', 3, NULL),
('1387584003.8300', 'Bli medlem', 'your-adress', 'gryta hagväg 6', 4, NULL),
('1387584003.8300', 'Bli medlem', 'your-zip', '76175', 5, NULL),
('1387584003.8300', 'Bli medlem', 'your-postal-area', 'norrtälje', 6, NULL),
('1387584003.8300', 'Bli medlem', 'your-phonenumber', '0123456789', 7, NULL),
('1387584004.8300', 'Bli medlem', 'has-wp-user', 'true', 0, NULL),
('1387584004.8300', 'Bli medlem', 'paid-membership-fee', '', 1, NULL),
('1387584004.8300', 'Bli medlem', 'your-name', 'Firstname Lastname', 2, NULL),
('1387584004.8300', 'Bli medlem', 'your-email', 'email@mail.se', 3, NULL),
('1387584004.8300', 'Bli medlem', 'your-adress', 'bergstigen 7', 4, NULL),
('1387584004.8300', 'Bli medlem', 'your-zip', '76192', 5, NULL),
('1387584004.8300', 'Bli medlem', 'your-postal-area', 'norrtälje', 6, NULL),
('1387584004.8300', 'Bli medlem', 'your-phonenumber', '0123456789', 7, NULL),
('1387584005.8300', 'Bli medlem', 'has-wp-user', '', 0, NULL),
('1387584005.8300', 'Bli medlem', 'paid-membership-fee', 'true', 1, NULL),
('1387584005.8300', 'Bli medlem', 'your-name', 'Firstname Lastname', 2, NULL),
('1387584005.8300', 'Bli medlem', 'your-email', 'email@mail.se', 3, NULL),
('1387584005.8300', 'Bli medlem', 'your-adress', 'robertsväg10 ', 4, NULL),
('1387584005.8300', 'Bli medlem', 'your-zip', '76045', 5, NULL),
('1387584005.8300', 'Bli medlem', 'your-postal-area', 'grisslehamn', 6, NULL),
('1387584005.8300', 'Bli medlem', 'your-phonenumber', '0123456789', 7, NULL);
答案 0 :(得分:1)
select
*
from
my_table m inner join (
select
m.*
from
my_table m
group by form_name, submit_time
having sum(
(field_name = 'has-wp-user' and field_value = '')
or
(field_name = 'paid-membership-fee' and field_value = 'true')
) = 2
) sq on m.form_name = sq.form_name and m.submit_time = sq.submit_time
where m.field_name in ('your-name', 'your-email');
它背后的想法是,每个field_name属于一个组。我在这里假设,它是form_name和submit_time列的组合。使用此子查询
select
m.*
from
my_table m
group by form_name, submit_time
having sum(
(field_name = 'has-wp-user' and field_value is null)
or
(field_name = 'paid-membership-fee' and field_value = 'true')
) = 2
然后只选择那些form_names,其中field_name行包含-wp-user和paid-membership-fee,值分别为null和true。现在,您可以再次加入原始表,只过滤掉允许的form_names,并方便地选择您需要的行。
答案 1 :(得分:0)
这不是最快的解决方案,因此如果性能问题我们可以解决这个问题。否则,这就是我要做的......
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(submit_time DECIMAL(13,2)
,form_name VARCHAR(30) NOT NULL
,field_name VARCHAR(30) NOT NULL
,field_value VARCHAR(30) NOT NULL
,field_order INT NOT NULL
,file INT NULL
,PRIMARY KEY(submit_time,form_name,field_name)
);
INSERT INTO my_table VALUES
(1387497601.83,'Bli medlem','has-wp-user','',0,NULL),
(1387497601.83,'Bli medlem','paid-membership-fee','true',1,NULL),
(1387497601.83,'Bli medlem','your-name','Williamsson',2,NULL),
(1387497601.83,'Bli medlem','your-email','Williamsson@mail.com',3,NULL),
(1387497601.83,'Bli medlem','your-adress','',4,NULL),
(1387497601.83,'Bli medlem','your-zip','',5,NULL),
(1387497601.83,'Bli medlem','your-postal-area','',6,NULL),
(1387497601.83,'Bli medlem','your-phonenumber','',7,NULL);
CREATE VIEW v_my_table AS
SELECT submit_time
, form_name
, MAX(CASE WHEN field_name = 'has-wp-user' THEN field_value END) has_wp_user
, MAX(CASE WHEN field_name = 'paid-membership-fee' THEN field_value END) paid_membership_fee
, MAX(CASE WHEN field_name = 'your-name' THEN field_value END) your_name
, MAX(CASE WHEN field_name = 'your-email' THEN field_value END) your_email
, MAX(CASE WHEN field_name = 'your-adress' THEN field_value END) your_address
, MAX(CASE WHEN field_name = 'your-zip' THEN field_value END) your_zip
, MAX(CASE WHEN field_name = 'your-postal-area' THEN field_value END) your_postal_area
, MAX(CASE WHEN field_name = 'your-phonenumber' THEN field_value END) your_phonenumber
FROM my_table
GROUP
BY submit_time
, form_name;
SELECT your_name
, your_email
FROM v_my_table
WHERE has_wp_user = ''
AND paid_membership_fee = 'true';
+------------+----------------------+
| your_name | your_email |
+------------+----------------------+
| Williamsson| Williamsson@mail.com |
+------------+----------------------+