如果条件为真,则选择数据
Posted
技术标签:
【中文标题】如果条件为真,则选择数据【英文标题】:Selecting data if condition is true 【发布时间】:2014-04-01 11:35:11 【问题描述】:我自己也无法解释这个问题,所以我创建了一张图片,因为它更容易理解。
表结构:
如果红色字段为空且绿色字段为真,我想从蓝色字段中选择数据。我该怎么做呢? 我已经为此苦苦挣扎了一段时间。
无法更改表结构,因为它是从 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'
伊什。
这条路有可能吗?
表结构:
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);
【问题讨论】:
考虑提供适当的 DDL 是否存在将这些行组合在一起的列,例如 user_id 之类的?如果是,那么您的问题已经解决了。 dev.mysql.com/doc/refman/5.1/de/if-statement.html 那么每个唯一的用户 id 都由 (submit_time, form_name) 简单定义?这似乎是一个相当弱的键 - 但我们可以使用它。请参阅下面的修正解决方案... 【参考方案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');
查看它在 sqlfiddle 中的实时工作(使用您的新数据更新)
其背后的想法是,每个 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 行 has-wp-user 和paid-membership-fee 的值分别为 null 和 true。现在您可以再次加入原始表,它只过滤掉允许的 form_names 并方便地选择您需要的行。
【讨论】:
【参考方案2】:这不是最快的解决方案,所以如果性能是一个问题,我们可以解决这个问题。否则,这就是我会做的......
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 |
+------------+----------------------+
【讨论】:
这正是我所需要的,谢谢!虽然,这如何与 my_table 中的多个人一起工作?因为我现在得到的视图只给了我一个条目,而其他字段(地址、邮编、邮政、电话号码)与电子邮件或姓名无关。还是因为某些paid_membership_fee 字段为空?无论如何,这绝对是我所需要的,再次感谢! 视图将为每个 form_name 创建一个单独的行。如果您想要其他内容,则必须详细说明您的数据是如何工作的。 删除实际用户数据后,我用 DDL 更新了第一篇文章 @Williamsson 我不得不再问一次,因为我没有得到答案:“是否有将这些行组合在一起的列,例如 user_id 之类的?”因为你的数据没有任何意义。例如,您如何知道电子邮件地址属于哪个用户?告诉我列名和/或看看我的答案。一旦我们知道了该列,这真的很简单。 @fancyPants 只能是(submit_time,form_name)以上是关于如果条件为真,则选择数据的主要内容,如果未能解决你的问题,请参考以下文章