它可能输出一个查询是可以在mysql中输入另一个sql查询

Short the query by join two or more tables

There are three tables

   login (id,name)
   visitor_detail(id,name)
   messaging_detail(id,visitor_id,message,send_by,created_at)
  • Relation on their are:
In `messaging_detail`.`visitor_id`=`visitor_detail`.`id` (compulsory always in relation)
 **But**
 `send_by` is may be some time (`messaging_detail`.`send_by` = `visitor_detail.id`) or (`messaging_detail`.`send_by` = `login.id`)
messaging_detail(table description)
id      visitor_id     message            send_by    created_at
-------------------------------------------------------------
1        7            dsssvdfsvfdsbvfdv   7           2015-04-11 13:08:49

Login(table description)
    id  name
    1   Admin

visitor_detail(table description)
id  name
1   Admin

  SELECT msg.*, 
    UNIX_TIMESTAMP(msg.`created_at`) AS unixTime,
    if(msg.visitor_id = msg.send_by,    
    FROM visitor_detail AS vs
    WHERE vs.`id`=msg.`send_by`,
    SELECT name
    FROM login AS log WHERE log.`id`=msg.`send_by`) AS msg_sender FROM `messaging_detail` AS msg WHERE msg.visitor_id='18'
    ORDER BY created_at ASC

please suggest best answer

You should use JOINS and UNION for such cases

SELECT * FROM (

SELECT msg.*,UNIX_TIMESTAMP(msg.`created_at`) AS unixTime,vs.name FROM  `messaging_detail` AS msg JOIN visitor_detail AS vs ON vs.id=msg.send_by WHERE msg.visitor_id = msg.send_by AND msg.visitor_id='18'

UNION

SELECT msg.*,UNIX_TIMESTAMP(msg.`created_at`) AS unixTime,vs.name FROM  `messaging_detail` AS msg JOIN login AS  vs ON vs.`id`=msg.`send_by` WHERE msg.visitor_id != msg.send_by AND msg.visitor_id='18'

) as a ORDER BY a.created_at ASC