Php 如果ID匹配,则从schools表中删除,否则为空。对于警报中的每一行,您是否只希望在警报类型=='claim'和null otherwire时返回值? SELECT a.id as alert_id,a.user_id,a.date,a.msg_titl

Php 如果ID匹配,则从schools表中删除,否则为空。对于警报中的每一行,您是否只希望在警报类型=='claim'和null otherwire时返回值? SELECT a.id as alert_id,a.user_id,a.date,a.msg_titl,php,mysql,Php,Mysql,如果ID匹配,则从schools表中删除,否则为空。对于警报中的每一行,您是否只希望在警报类型=='claim'和null otherwire时返回值? SELECT a.id as alert_id,a.user_id,a.date,a.msg_title,a.message,a.alert_type,a.school_or_contact_id, u.id as user_id, u.full_name, c.id as contact_id, concat(c.f_name,' ',c.l

如果ID匹配,则从schools表中删除,否则为空。对于警报中的每一行,您是否只希望在警报类型=='claim'和null otherwire时返回值?
SELECT a.id as alert_id,a.user_id,a.date,a.msg_title,a.message,a.alert_type,a.school_or_contact_id,
u.id as user_id, u.full_name,
c.id as contact_id, concat(c.f_name,' ',c.l_name) as contact_name
FROM alerts a
LEFT OUTER JOIN users u ON a.user_id = u.id
LEFT OUTER JOIN contacts c ON a.school_or_contact_id = c.id
LEFT OUTER JOIN schools s ON a.school_or_contact_id = s.school_id
ORDER BY a.date
SELECT a.id as alert_id,a.user_id,a.date,a.msg_title,a.message,a.alert_type,a.school_or_contact_id,
u.id as user_id, u.full_name,
c.id as contact_id, concat(c.f_name,' ',c.l_name) as contact_name,
IF(a.alert_type = 'claim', select s.* from schools where school_id = a.school_or_contact_id)
FROM alerts a
LEFT OUTER JOIN users u ON a.user_id = u.id
LEFT OUTER JOIN contacts c ON a.school_or_contact_id = c.id
LEFT OUTER JOIN schools s ON a.school_or_contact_id = s.school_id
ORDER BY a.date
mysql> desc alerts;
+----------------------+--------------+------+-----+-------------------+----------------+
| Field                | Type         | Null | Key | Default           | Extra          |
+----------------------+--------------+------+-----+-------------------+----------------+
| id                   | int(11)      | NO   | PRI | NULL              | auto_increment | 
| user_id              | int(12)      | YES  |     | NULL              |                | 
| date                 | timestamp    | NO   |     | CURRENT_TIMESTAMP |                | 
| msg_title            | varchar(100) | YES  |     | NULL              |                | 
| message              | longtext     | YES  |     | NULL              |                | 
| alert_type           | varchar(255) | YES  |     | NULL              |                | 
| school_or_contact_id | int(12)      | YES  |     | NULL              |                | 
+----------------------+--------------+------+-----+-------------------+----------------+
7 rows in set (0.00 sec)
+----------------------+--------------+------+-----+-------------------+----------------+
| Field                | Type         | Null | Key | Default           | Extra          |
+----------------------+--------------+------+-----+-------------------+----------------+
| id                   | int(11)      | NO   | PRI | NULL              | auto_increment | 
| user_id              | int(12)      | YES  |     | NULL              |                | 
| date                 | timestamp    | NO   |     | CURRENT_TIMESTAMP |                | 
| msg_title            | varchar(100) | YES  |     | NULL              |                | 
| message              | longtext     | YES  |     | NULL              |                | 
| alert_type           | varchar(255) | YES  |     | NULL              |                | 
| school_id            | int(12)      | YES  |     | NULL              |                |
| contact_id           | int(12)      | YES  |     | NULL              |                |
+----------------------+--------------+------+-----+-------------------+----------------+
SELECT a.id as alert_id,a.user_id,a.date,a.msg_title,a.message,a.alert_type,a.school_or_contact_id,
u.id as user_id, u.full_name,
c.id as contact_id, concat(c.f_name,' ',c.l_name) as contact_name, s.*
FROM alerts a
LEFT OUTER JOIN users u ON a.user_id = u.id
LEFT OUTER JOIN contacts c ON a.school_or_contact_id = c.id
LEFT OUTER JOIN schools s ON a.school_or_contact_id = s.school_id AND a.alert_type = 'claim'
ORDER BY a.date
SELECT a.id as alert_id,a.user_id,a.date,a.msg_title,a.message,a.alert_type,a.school_or_contact_id,
u.id as user_id, u.full_name,
c.id as contact_id, concat(c.f_name,' ',c.l_name) as contact_name,
IF (a.alert_type = 'claim', s.col1, NULL) AS col1,
IF (a.alert_type = 'claim', s.col2, NULL) AS col2
FROM alerts a
LEFT OUTER JOIN users u ON a.user_id = u.id
LEFT OUTER JOIN contacts c ON a.school_or_contact_id = c.id
LEFT OUTER JOIN schools s ON a.school_or_contact_id = s.school_id
ORDER BY a.date
SELECT * FROM
  (SELECT a.id as alert_id,a.user_id,a.date,a.msg_title,a.message,
  a.alert_type,a.school_or_contact_id,
  u.id as user_id, u.full_name,
  c.id as contact_id, concat(c.f_name,' ',c.l_name) as contact_name,NULL,NULL
  FROM alerts a
  LEFT OUTER JOIN users u ON a.user_id = u.id
  LEFT OUTER JOIN contacts c ON a.school_or_contact_id = c.id
  WHERE xa.alert_type!='claim'
  UNION SELECT xa.id as alert_id,xa.user_id,xa.date,xa.msg_title,xa.message,
  xa.alert_type,xa.school_or_contact_id,
  xu.id as user_id, xu.full_name,
  xc.id as contact_id, concat(xc.f_name,' ',xc.l_name) as contact_name,xs.foo,xs.bar
  FROM alerts xa
  LEFT OUTER JOIN users xu ON xa.user_id = xu.id
  LEFT OUTER JOIN contacts xc ON xa.school_or_contact_id = xc.id
  LEFT OUTER JOIN schools xs ON xa.school_or_contact_id = xs.school_id
  WHERE xa.alert_type='claim')
ORDER BY date
SELECT a.id as alert_id
     , a.user_id
     , a.date
     , a.msg_title
     , a.message
     , a.alert_type
     , a.school_or_contact_id
     , u.id as user_id
     , u.full_name
     , c.id as contact_id
     , concat(c.f_name,' ',c.l_name) as contact_name
     , s.* 
FROM alerts a
FROM alerts a
  LEFT OUTER JOIN users u 
    ON  a.user_id = u.id
  LEFT OUTER JOIN contacts c 
    ON  a.school_or_contact_id = c.id
  LEFT OUTER JOIN schools s 
    ON  a.school_or_contact_id = s.school_id
    AND a.alert_type = 'claim' 
ORDER BY a.date