I need to do a MYSQL query to select users from "table_in" which has the same format as table_1
table_1
id Field_id user_id value
1 9 1 "hello"
2 10 1 "Multi"
3 9 2 "Something"
4 10 2 "Single"
table_2
id user_id status specs
1 1 "Busy" "this is a test"
2 2 "Idle" "this is another test"
3 2 "Busy" "another test again"
4 1 "Relaxing" "something else"
I need to select users from table_in, where either one of the following is true
- table_1 field_id=10 contains "Multi"
- table_2 status is NOT Busy
Then save the result in a new table table_out
So what i got so far is this
CREATE TEMPORARY TABLE table_out
SELECT * FROM table_1 WHERE user_id in (SELECT user_id from table_in)
AND (field_id=10 AND value='Multi')
OR (SELECT * from table_2 where status!='Busy')
Im not SQL expert and the MY-SQL statement i have above dont work. I think maybe the last OR statement is wrong where its trying to select from table_2 where the status is not busy.
So basically the users that gets put into table_out must all come from "table_in" and either have (field 10 set to multi), or the same user in table_2 (status!=Busy)
Does anybody know how to do it?
UPDATE: Found my own solution. Using table_in, create a second table by selecting only those values from it that is not "multi", then from that query table_2 for those user_id that are not busy put that in another table, then remove those values from table_in.
Thanks anyway