I have this query:
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 This works, but I need it to do one more thing, and I can't seem to figure it out. I need to select some data from the "schools" table IF data in alerts.alert_type (alerts table) == "claim".
If "claim" is not found in alerts.alerts_table, then it needs to do nothing different than the query above. alerts.alert_table
This is what I've tried, but it doesn't seem to work:
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 EDIT For clarification, I'm building a tool that has front page "update" kind of like Facebook. Depending on what the users are doing, the "alerts" will say different things.
The schools table has 3,000 rows and will only apply to the alerts table when the row alerts_type.alerts == "claim". Otherwise, it won't matter what what's in the schools table. If alert_type.alerts != "claim", the "contacts" table will be where the rest of the data comes from.
I wanted to have cleaner data when doing the query (ie -- not "school" table data when alerts_type.alerts != "claim") but I can easily do this in PHP. I just didn't want to pull data that I wouldn't use.
Thank you everyone for all the help and advice!
2nd edit I will change the table schema. Right now, it looks like this:
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) I will edit the alerts table to this (below), then JOIN alerts.school_id = schools.school_id. This should fix the problem.
+----------------------+--------------+------+-----+-------------------+----------------+ | 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 | | +----------------------+--------------+------+-----+-------------------+----------------+