1

我目前有两张票。表 1 是票证,表 2 是响应。

我目前做

select id, subject, description 
from tickets 
where subject like '%hello%';

这可行,但我也想返回相同的字段,但 hello 与响应表中的消息字段匹配。

例如,我希望我能做到:

select id, subject, description 
from tickets, responses 
where tickets.subject like '%hello%' OR responses.message like '%hello%'; 

响应表中的ticketid 与tickets 表中的id 相同。

4

4 回答 4

0

如果我理解正确,您只需要门票表中的结果吗?您可以使用tableName.*或使用 tableName.columnName 来仅从该原始表中获取结果。

select tickets.id, tickets.subject, tickets.description 
from tickets, responses 
where tickets.id = responses.id
AND (tickets.subject like '%hello%' 
OR responses.message like '%hello%')
于 2012-06-11T14:20:54.553 回答
0

您可能需要左连接 -

select id, subject, description 
from tickets left join responses on tickets.id = responses.ticketid
where tickets.subject like '%hello%' OR responses.message like '%hello%';

[更正] 左连接

于 2012-06-11T14:22:06.560 回答
0

您需要加入表格:

SELECT `tickets`.`id`, `tickets`.`subject`, `tickets`.`description`, `responses`.`message` 
LEFT JOIN `responses` ON `tickets`.`id` = `responses`.`id` 
WHERE `tickets`.`subject` like '%hello%' OR responses.message like '%hello%'
于 2012-06-11T14:23:02.860 回答
0

如果总是有响应记录:

  select tickets.id, tickets.subject, tickets.description 
  from tickets, responses 
  where
    tickets.id = responses.id and
    ( tickets.subject like '%hello%' or 
      responses.message like '%hello%' )

如果票证可以在没有响应的情况下存在:

  select tickets.id, tickets.subject, tickets.description 
  from tickets left outer join responses on tickets.id = responses.id
  where
    tickets.subject like '%hello%' or 
    responses.message like '%hello%'
于 2012-06-11T14:23:03.210 回答