0

我的查询为每张票提供多行。我想在一行中列出所有自定义值。当我查询我的数据库时,我希望val.value每个列都列为列,而不是每个列的多行ticket_idticket_id

我希望输出显示val.value为列。

SELECT 
ticket.ticket_id,ticket.`number`,ticket.dept_id,val.field_id,val.value,
ticket.staff_id,ticket.team_id,user.name,   dept.dept_name,status.state,status.name as 
status,ticket.source,ticket.isoverdue,ticket.isanswered,ticket.created 
FROM ost_TICKET ticket 
LEFT JOIN ost_ticket_status status ON status.id = ticket.status_id 
LEFT JOIN ost_user user ON user.id = ticket.user_id 
LEFT JOIN ost_department dept ON ticket.dept_id=dept.dept_id 
LEFT JOIN ost_form_entry entry ON ticket.ticket_id = entry.object_id 
LEFT JOIN ost_form_entry_values val ON entry.id = val.entry_id

输出如下:

ticket_id,number,dept_id,field_id,value,staff_id,team_id,name,dept_name,state,status,source,isoverdue,isanswered,created

4, 587202,4,3,NULL,15,0,Mark D,SalesDep,open,Open,Phone,0,0,10/14/2014 9:09 4, 587202,4,4,NULL,15,0 ,Mark D,SalesDep,open,Open,Phone,0,0,10/14/2014 9:09 4, 587202,4,87,NULL,15,0,Mark D,SalesDep,open,Open,Phone,0 ,0,10/14/2014 9:09 4, 587202,4,117,NULL,15,0,Mark D,SalesDep,open,打开,电话,0,0,10/14/2014 9:09 4, 587202, 4,20,Fish Farm,15,0,Mark D,SalesDep,open,Open,电话,0,0,10/14/2014 9:09 4, 587202,4,22,Normal,15,0,Mark D ,SalesDep,open,Open,Phone,0,0,10/14/2014 9:09 4, 587202,4,73,nya-08-211,15,0,Mark D,SalesDep,open,Open,Phone, 0,0,10/14/2014 9:09 4, 587202,4,74,88,15,0,Mark D,SalesDep,open,Open,Phone,0,0,10/14/2014 9:09 4 , 587202,4,84,Bara,15,0,Mark D,SalesDep,open,Open,Phone,0,0,10/14/2014 9:09 4, 587202,4,85,Shaba,15,0, Mark D,SalesDep,open,Open,Phone,0,0,10/14/2014 9:09 4, 587202,4,86,Kolkata,15,0,Mark D,SalesDep,open,Open,Phone,0, 0,10/14/2014 9:09 4, 587202,4,165,Tango,15,0,Mark D,SalesDep,open,Open,Phone,0,0,10/14/2014 9:09

4

2 回答 2

0

这只是一个猜测(因为 OP 没有提供表模式):

如果在其中一张表中,JOIN1 个工单行有几行,那么一个工单将有双行(例如 2 封电子邮件)。

查询也有错误的连接:

LEFT JOIN ost_email email ON user.id = email.email_id

我想应该是:

LEFT JOIN ost_email email ON user.id = email.user_id

编辑: 您可以尝试使用DISTINCT,但您必须检查结果中的数据是否正常。

于 2015-08-06T10:07:10.043 回答
0

应该是这样的

SELECT t.ticket_id,t.number,t.dept_id,v.field_id,v.value,t.staff_id,t.team_id,u.name,d.name,s.state,s.name status,t.source,t.isoverdue,t.isanswered,t.created FROM ost_ticket t,ost_department d,ost_ticket_status s,ost_user u,ost_form_entry e,ost_form_entry_values v where s.id = t.status_id and u.id = t.user_id and t.dept_id=d.id and t.ticket_id = e.object_id and e.id = v.entry_id
于 2017-05-01T10:49:57.333 回答