将2个sql列合并到一个文本框中 [英] Merging 2 sql columns into one textbox

查看:63
本文介绍了将2个sql列合并到一个文本框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用数据库创建一个信使,我一直试图检索邮件。这就是我到目前为止。

 strQuery =  选择消息FROM chat WHERE chat.from ='& TextBox5.Text&  'AND chat.to ='& TextBox3.Text&  ' 



查询的作用是从数据库中检索消息,textbox5是发件人,textbox3是检索器。目前它只显示另一方发送给您的消息,我让它工作正常。但我希望它能同时显示已发送的消息和您的消息。例如:

 Tom J。:嘿,伙计,怎么了? 
我:没什么,你自己怎么样?



它目前只显示已发送的消息。数据库结构如下:

ID(消息ID)消息(消息区域)从(发件人ID)到(检索者ID)



我希望能够检索FROM和TO,并使用richtextbox按顺序显示它们。这可能吗?如果是这样我将如何做到这一点?

解决方案

你可以像这样连接查询结果

  SELECT  concat(message_from,' ',message_to) AS 消息 FROM  chat; 



这是MySQL语法,但语法在其他方面应该类似。



我同意Mehdi,但是,你应该考虑改变你的设计。


你需要一个新的模型,比如对话和对话细节,其中对话有一个参与者列表(聊天中你可以有两个以上的人)和一个ID和对话详细信息有一个日期时间,UserId,消息,ConversationId等。


试试这个



< pre lang =SQL> SELECT userName + ' :' + [Message] as 消息 FROM chat
INNER JOIN userTbl ON userTbl.userId = chat。[来自]





如果你有日期字段然后按日期字段使用订单desc





祝你好运; - )


I'm creating a messenger using a database and I'm stuck on trying to retrieve messages. This is what I have so far.

strQuery = "SELECT message FROM chat WHERE chat.from ='" & TextBox5.Text & "' AND chat.to ='" & TextBox3.Text & "'"


What the query does is retrieve messages from the database with textbox5 being the sender and textbox3 being the retriever. Currently it only displays messages sent to you by the other party and I have it working fine. But I want it to display both sent messages and your messages. For example:

Tom J.: Hey man, what's up?
Me: Nothing much, how about yourself?


It's currently only showing sent messages. The database structure is of the following:
ID (Message ID) Message (Message area) From (ID of sender) To (ID of retriever)

I want to be able to retrieve both, FROM and TO and display them in order using a richtextbox. Is this possible? If so how would I go about doing this?

解决方案

You can concatenate the result from your query like this

SELECT concat(message_from, ' ', message_to) AS message FROM chat;


This is MySQL syntax, but the syntax should be similar in other flavors.

I agree with Mehdi, though, that you should consider to change your design.


You need a new model for this, like "Conversation" and "Conversation Details" where the "Conversation" has a list of participants (you can have more than 2 people in the chat) and an ID and the "Conversation Details" has a datetime, UserId, Message, ConversationId etc.


try this

SELECT userName +' : '+ [Message] as message FROM chat
INNER JOIN userTbl ON userTbl.userId=chat.[from]



if u have a date field then use order by datefield desc


good luck ;-)


这篇关于将2个sql列合并到一个文本框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆