我目前有2张票证表。表1是门票,表2是回复。
我目前正在做
select id, subject, description
from tickets
where subject like '%hello%';
这有效,但我也希望返回相同的字段,但hello与response表中的message字段匹配。
E.g。我希望我能做到:
select id, subject, description
from tickets, responses
where tickets.subject like '%hello%' OR responses.message like '%hello%';
响应表中的ticketid与故障单表中的id相同。
答案 0 :(得分: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%')
答案 1 :(得分: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%';
[更正] LEFT JOIN
答案 2 :(得分: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%'
答案 3 :(得分: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%'