mysql 中的 CONCAT 与 GROUP_CONCAT
所以我到处寻找这个问题的答案,但到目前为止还没有运气。非常感谢任何帮助。我是 mysql 新手(从 filemaker pro 背景转移)。
我有一个数据库,用于保存我们业务前景的信息。我有一个关于列出信息供用户查看的问题。 INQUIRY 表保存每个查询的信息(即查询进入的日期、查询的状态等)。 PROSPECT 表保存每个潜在客户(潜在客户)的信息(即名字、姓氏、年龄等)。第三个表 INQUIRYNOTES 保存我们与潜在客户的联系信息(即下次联系日期、备注、下一步行动等)。这些表中的每一个都与键 inquiryID 相关。
每天,用户都会来到一个屏幕,在那里他们将看到他/她当天将跟进的线索/询问,这些询问的行动日期小于或等于今天。我希望列表包含inquiryID、列出与该inquiryID 相关的所有潜在客户的名称(最多 2 个)以及操作日期。
这是我到目前为止所尝试过的:
SELECT inquiry.inquiryID,
CASE WHEN prospect.lastName = '' THEN prospect.firstName
WHEN prospect.firstName = '' THEN prospect.lastName
ELSE CONCAT(prospect.lastName, ', ',prospect.firstName) END as fullName, inquiryNote.actionDate
FROM inquiry, prospect, inquiryNote
WHERE inquiry.inquiryID = prospect.inquiryID
AND inquiry.inquiryID = inquirynote.inquiryID
AND inquirynote.actionDate != \"0000-00-00\"
AND actionDate <= \"".date("Y-m-d")."\"
GROUP BY inquiryID ORDER BY actionDate";
这会返回我正在查找的信息,但它只列出了其中一个潜在客户(即使有 2 个潜在客户可供查询)。
接下来,我尝试将 GROUP_CONCAT(firstname) 放入其中,它给了我一个以逗号分隔的行中列出的潜在客户的名字列表(我越来越接近了)。然后我尝试了 GROUP_CONCAT(last name, ', ', firstname) ,然后尝试了 GROUP_CONCAT(lastname,firstname) ,但它再次只列出一个潜在客户,而不是用逗号分隔的多个潜在客户。
我希望我在上面的代码中命名为 fullName 的变量包含在 GROUP_CONCAT 中,以便在返回的行中我看到与列出的查询相关的所有潜在客户的列表并用逗号分隔。
我希望我创建的网页中的表格包含以下列:
inquiryID | 潜在客户姓名(姓氏在前)| 下一步操作日期
到目前为止,我再次可以似乎只得到:
inquiryID | 1 潜在客户名称| 下一步行动日期
解决方案可能很简单,但由于我是 mysql 的新手,所以我迷失了。
如果您需要更多详细信息,请告诉我。
更新解决方案,但最后有类似的问题
问题是某些名字和姓氏字段具有 NULL 值。显然,GROUP_CONCAT 现在可以使用空值。这是我在摆脱所有 NULL 值后结束的查询:
SELECT
inquiry.inquiryID,
inquiry.initialDate,
inquiryNote.actionDate,
GROUP_CONCAT(DISTINCT
CONCAT(
CASE WHEN prospect.lastName = '' THEN prospect.firstName
WHEN prospect.firstName = '' THEN prospect.lastName
ELSE CONCAT(
prospect.firstName, ' ',prospect.lastName
) END
) SEPARATOR ', '
) AS prospectList,
GROUP_CONCAT(DISTINCT
CONCAT(
CASE WHEN influencer.lastName = '' THEN influencer.firstName
WHEN influencer.firstName = '' THEN influencer.lastName
ELSE CONCAT(
influencer.firstName, ' ',influencer.lastName
) END
) SEPARATOR ', '
) AS influencerList
FROM inquiry, prospect, inquiryNote, influencer WHERE inquiry.inquiryID = prospect.inquiryID
AND inquiry.inquiryID = inquirynote.inquiryID
AND inquiry.inquiryID = influencer.inquiryID
AND inquirynote.actionDate != "0000-00-00" AND actionDate <= "2011-09-22"
GROUP BY inquiryID
ORDER BY actionDate;
所以最后的问题是,即使遇到 NULL 值,我也可以使 GROUP_CONCAT 工作吗?
So I have looked everywhere for an answer to this, but so far no luck. Any help is much appreciated. I am new to mysql (transferring from a filemaker pro background).
I have a database where I keep information for prospects for our business. I have a question about listing information for our users to see. The INQUIRY table holds the information for each inquiry (i.e. date inquiry came in, status of the inquiry, etc.). The PROSPECT table holds the information on each prospect (potential client) (i.e. first name, last name, age, etc.). A third table, INQUIRYNOTES, hold information on our contacts with the prospect (i.e. next contact date, notes, next action, etc.). Each of these tables are related with the key inquiryID.
Each day a user will come to a screen where they will be presented with the leads/inquiries that he/she will follow up on for that day which are those inquiries who's action date is less than or equal to today. I want the list to contain the inquiryID, listed names of all prospects related to that inquiryID (max 2), and the action date.
Here is what I've tried so far:
SELECT inquiry.inquiryID,
CASE WHEN prospect.lastName = '' THEN prospect.firstName
WHEN prospect.firstName = '' THEN prospect.lastName
ELSE CONCAT(prospect.lastName, ', ',prospect.firstName) END as fullName, inquiryNote.actionDate
FROM inquiry, prospect, inquiryNote
WHERE inquiry.inquiryID = prospect.inquiryID
AND inquiry.inquiryID = inquirynote.inquiryID
AND inquirynote.actionDate != \"0000-00-00\"
AND actionDate <= \"".date("Y-m-d")."\"
GROUP BY inquiryID ORDER BY actionDate";
This returns the information that I am looking for, but it only lists one of the prospect (even if there are 2 for the inquiry).
Next I tried putting the GROUP_CONCAT(firstname) in this and it gave me a list of the first names of the prospect listed in a row separated by commas (I'm getting closer). Then I tried GROUP_CONCAT(last name, ', ', firstname) and then GROUP_CONCAT(lastname,firstname), but again it would only list one prospect, not the multiple prospects separated by commas.
I want the variable that I named fullName in the code above to be included in the GROUP_CONCAT so that in the rows returned I see a list of all prospect related to an inquiry listed and divided by a comma.
I want the table in the webpage I have created to have these columns:
inquiryID | prospects name(s) (last name first) | Next Action Date
Again so far I can only seem to get:
inquiryID | 1 prospect name | Next Action Date
The solution could be something easy, but since I am new to mysql I am lost.
Please let me know if you need more detail.
UPDATE WITH SOLUTION, BUT SIMILAR QUESTION AT THE END
The problem was that some of the first name and last name fields had NULL values. Apparently, GROUP_CONCAT will now work with null values. This is the query that I ended with that worked once I got rid of all of the NULL values:
SELECT
inquiry.inquiryID,
inquiry.initialDate,
inquiryNote.actionDate,
GROUP_CONCAT(DISTINCT
CONCAT(
CASE WHEN prospect.lastName = '' THEN prospect.firstName
WHEN prospect.firstName = '' THEN prospect.lastName
ELSE CONCAT(
prospect.firstName, ' ',prospect.lastName
) END
) SEPARATOR ', '
) AS prospectList,
GROUP_CONCAT(DISTINCT
CONCAT(
CASE WHEN influencer.lastName = '' THEN influencer.firstName
WHEN influencer.firstName = '' THEN influencer.lastName
ELSE CONCAT(
influencer.firstName, ' ',influencer.lastName
) END
) SEPARATOR ', '
) AS influencerList
FROM inquiry, prospect, inquiryNote, influencer WHERE inquiry.inquiryID = prospect.inquiryID
AND inquiry.inquiryID = inquirynote.inquiryID
AND inquiry.inquiryID = influencer.inquiryID
AND inquirynote.actionDate != "0000-00-00" AND actionDate <= "2011-09-22"
GROUP BY inquiryID
ORDER BY actionDate;
So the final question is, can I make GROUP_CONCAT work even when it encounters NULL values?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果列包含 NULL,您可以合并列:
如果姓氏为空,则 COALESCE(lastname, '') 将返回一个空字符串,如果姓氏不为空,则返回任何值。
You can COALESCE the columns in case the contain NULLs:
COALESCE(lastname, '') would return an empty string if lastname was null, or whatever the value of lastname is if it is not.
GROUP_CONCAT 只是忽略(跳过)NULL 值 http://thinkdiff.net/mysql/mysql- the-group_concat-function/
GROUP_CONCAT simply ignores(skips) NULL values http://thinkdiff.net/mysql/mysql-the-group_concat-function/