我正在尝试创建一个需要多个WHERE条件的SELECT语句。那可能吗。这是场景。我有一个具有以下结构的表。
FormID(类型Int)
SubmissionID(类型Int)
FieldName(类型 文本)
FieldValue(类型文本)
数据会是这样的。 表名: SubmissionsValue
FormID - SubmissionID - FieldName - FieldValue 24 ------ 5 ---------- username ------ johnk 24 ------ 5 ---------- firstname ------ John 24 ------ 5 ---------- lastname ------ King 24 ------ 5 ---------- address ------ 100 some street 24 ------ 5 ---------- email ------ johnemail@gma.com 24 ------ 9 ---------- username ------ cathym 24 ------ 9 ---------- firstname ------ Catherine 24 ------ 9 ---------- lastname ------ Mirusia 24 ------ 9 ---------- address ------ 100 some other street 24 ------ 9 ---------- email ------ cathyemail@gma.com 32 ------ 20 ---------- username ------ johnk 32 ------ 20 ---------- firstname ------ John 32 ------ 20 ---------- lastname ------ King 32 ------ 20 ---------- address ------ 100 some street 32 ------ 20 ---------- email ------ johnemail@gma.com 32 ------ 20 ---------- AdultTicket------ 5 32 ------ 20 ---------- SingleTicket------ 3 32 ------ 20 ---------- Total ------ $60.00
显然这是实体 - 属性 - 价值(EAV)设计,我被告知了!
正如您所见," FieldName"," FieldValue" etc是表的Fields的实际名称。此外,来自同一用户的多个表单的条目也可以显示在表中。所以我的键区是用户emailID和formID 。
然后,字段名称与每个在线表单的字段值一起存储在表格中。
因此"用户名","名字","姓氏","地址"和"电子邮件"是表单中的实际字段,当用户提交信息时,它将存储在表中,如上所示。
我想使用他们的emailID作为关键字段在另一个区域显示用户的详细信息。
基本上这就是必须发生的事情($ user-> email给出了当前用户的电子邮件):
SELECT username WHERE (FieldName = "email") AND (FieldValue = " $user->email")
但实际上要获得价值" johnk "和" cathym "在我的例子中,我似乎要做的是以下内容,但我不知道如何编写代码:
SELECT FieldValue WHERE (FieldName = "username")
(WHERE (FieldName = "email")
AND (FieldValue = " $user->email"))
有人可以帮我这个脚本吗?
这是使用RSForm!Pro的Joomla网站。 RSForm!Pro以上述方式存储表单输入!!!
===编辑====
澄清:上面显示的表数据全部来自一个表。为了便于阅读,我在两组数据之间添加了一个空格。
您基本上可以忽略FormID和SubmissionID。后端将来自各种不同表单的条目保存在一个表中,因此它跟踪formID和submissionID。所以你可以看到最终会在该表中有各种formID和submissionID。
在这个例子中,数据是从同一表格输入的,但有两个不同的提交 - 5& 8.
在这种情况下,我只想使用登录用户的信息自动填充新表单的字段。
Last Name: John King
User Address: 100 Some Street
etc...
答案 0 :(得分:2)
你可以自己加入桌子:
SELECT t2.FieldValue FROM <table> t1
INNER JOIN <table> t2 ON t1.formId = t2.formId
WHERE t1.fieldName = 'email'
AND t1.fieldValue = '$user->email'
AND t2.fieldName = 'username'
我认为这会给你想要的结果,它是自己加入同一个表,所以你能够返回正确的值。不幸的是我现在无法测试这个,但希望这就是你所追求的。
基本上表只能返回x行,并且行不能引用其他返回的行(据我所知),因此不是引用Null值,而是设置表自我加入(t1
,t2
)FormId
值(所有关联行之间的连接),以便FormId
每个WHERE
要检查的数据{{ 1}}子句已经是每个FormId的所有行。
这在您当前的SQL中是不可能的,因为您不知道formID并且无法找到它(也可能通过子选择完成,但我没有接受该路由)。
因此,新SQL的第一部分返回FormId = number
所在的整个块,然后检查该块的email address = name@whatever
,然后返回相应的用户名,在本例中。
我希望这会有所帮助。
请同时研究准备好的语句以及如何正确编写PHP和SQL代码,而不会让您的数据库受到滥用和风险。 Please read this link由终点站发布。 PHP The Right Way也是这个主题的一个很好的参考。
答案 1 :(得分:1)
解决此问题的一种可能性是使用子查询。
SELECT t.FieldValue
FROM table t
WHERE t.FormID = (
SELECT min(FormID)
FROM table
WHERE (FieldName = "email")
AND (FieldValue = "$user->email")
) AND t.SubmissionID = (
SELECT min(SubmissionID)
FROM table
WHERE (FieldName = "email")
AND (FieldValue = "$user->email")
) AND t.FieldName ="username"
另一种可能性就是加入桌子。
SELECT u.FieldValue
FROM table u
JOIN table e ON u.FormID = e.FormID
AND u.submissionID = e.submissionID
WHERE e.FieldName ="email"
AND e.FieldValue = "$user->email"
AND u.FieldName = "username"
子查询 - 解决方案可能会更慢,具体取决于MySQL如何优化查询。
答案 2 :(得分:1)
数据集
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(formID INT NOT NULL -- Cannot be NULL
,SubmissionID INT NOT NULL -- Cannot be NULL
,FieldName VARCHAR(20) NOT NULL -- Cannot be NULL
,FieldValue VARCHAR(100) NOT NULL -- Could be NULL. Whether or not this can be NULL has important consequences for some of what follows!!
,PRIMARY KEY(formid,submissionid,fieldname)
);
INSERT INTO my_table VALUES
(24,5,'username', 'johnk'),
(24,5,'firstname','John'),
(24,5,'lastname', 'King'),
(24,5,'address', '100 some street'),
(24,5,'email', 'johnemail@gma.com'),
(24,9,'username', 'cathym'),
(24,9,'firstname','Catherine'),
(24,9,'lastname', 'Mirusia'),
(24,9,'address', '100 some other street'),
(24,9,'email','cathyemail@gma.com');
例如:
SELECT x.formid
, x.submissionid
, MAX(CASE WHEN fieldname = 'username' THEN fieldvalue END) username
, MAX(CASE WHEN fieldname = 'firstname' THEN fieldvalue END) firstname
, MAX(CASE WHEN fieldname = 'lastname' THEN fieldvalue END) lastname
, MAX(CASE WHEN fieldname = 'address' THEN fieldvalue END) address
, MAX(CASE WHEN fieldname = 'email' THEN fieldvalue END) email
FROM my_table x
GROUP
BY formid
, submissionid
HAVING email = 'johnemail@gma.com';
+--------+--------------+----------+-----------+----------+-----------------+-------------------+
| formid | submissionid | username | firstname | lastname | address | email |
+--------+--------------+----------+-----------+----------+-----------------+-------------------+
| 24 | 5 | johnk | John | King | 100 some street | johnemail@gma.com |
+--------+--------------+----------+-----------+----------+-----------------+-------------------+
或
SELECT x.formid
, x.submissionid
, x.fieldvalue email
, MAX(CASE WHEN y.fieldname = 'username' THEN y.fieldvalue END) username
, MAX(CASE WHEN y.fieldname = 'firstname' THEN y.fieldvalue END) firstname
, MAX(CASE WHEN y.fieldname = 'lastname' THEN y.fieldvalue END) lastname
, MAX(CASE WHEN y.fieldname = 'address' THEN y.fieldvalue END) address
FROM my_table x
JOIN my_table y
ON y.formid = x.formid
AND y.submissionid = x.submissionid
AND y.fieldname <> x.fieldname
WHERE x.fieldname= 'email'
AND x.fieldvalue = 'johnemail@gma.com'
GROUP
BY formid
, submissionid
, email;
+--------+--------------+----------+-----------+----------+-----------------+-------------------+
| formid | submissionid | username | firstname | lastname | address | email |
+--------+--------------+----------+-----------+----------+-----------------+-------------------+
| 24 | 5 | johnk | John | King | 100 some street | johnemail@gma.com |
+--------+--------------+----------+-----------+----------+-----------------+-------------------+
或
SELECT email.formid
, email.submissionid
, email.fieldvalue email
, username.fieldvalue username
, firstname.fieldvalue firstname
, lastname.fieldvalue lastname
, address.fieldvalue address
FROM my_table email
LEFT
JOIN my_table username
ON username.formid = email.formid
AND username.submissionid = email.submissionid
AND username.fieldname = 'username'
LEFT
JOIN my_table firstname
ON firstname.formid = email.formid
AND firstname.submissionid = email.submissionid
AND firstname.fieldname = 'firstname'
LEFT
JOIN my_table lastname
ON lastname.formid = email.formid
AND lastname.submissionid = email.submissionid
AND lastname.fieldname = 'lastname'
LEFT
JOIN my_table address
ON address.formid = email.formid
AND address.submissionid = email.submissionid
AND address.fieldname = 'address'
WHERE email.fieldname = 'email'
AND email.fieldvalue = 'johnemail@gma.com';
+--------+--------------+-------------------+----------+-----------+----------+-----------------+
| formid | submissionid | email | username | firstname | lastname | address |
+--------+--------------+-------------------+----------+-----------+----------+-----------------+
| 24 | 5 | johnemail@gma.com | johnk | John | King | 100 some street |
+--------+--------------+-------------------+----------+-----------+----------+-----------------+
编辑:
如果您只想返回所有字段&#39;与给定用户提交相关联,可能如下所示,但请注意,通常用户数据通常会保存在专用表中,即使我确信,使用像Joomla这样的框架时:
SELECT a.*
FROM my_table a
JOIN my_table b
ON b.formid = a.formid
AND b.submissionid = a.submissionid
WHERE b.fieldname = 'email'
AND b.fieldvalue = 'johnemail@gma.com';
+--------+--------------+-----------+-------------------+
| formID | SubmissionID | FieldName | FieldValue |
+--------+--------------+-----------+-------------------+
| 24 | 5 | address | 100 some street |
| 24 | 5 | email | johnemail@gma.com |
| 24 | 5 | firstname | John |
| 24 | 5 | lastname | King |
| 24 | 5 | username | johnk |
+--------+--------------+-----------+-------------------+
答案 3 :(得分:0)
首先,获取与子查询中的电子邮件对应的formId和SubmissionId,并从获取的formId和SubmissionId中获取用户名
SELECT FieldValue FROM table t1,
(SELECT FormID, SubmissionID FROM table
WHERE FieldName='email' AND FieldValue = '$user->email')
t2 WHERE t1.SubmissionID = t2.SubmissionID
AND t1.FormId = t2.FormID
AND t1.FieldName = 'username'