I want to insert a row into a table based on some condition that needs to be in the query itself.

Let's say I have two tables `foo` and `bar`. Each have an `id` column.

Now I have a third table `myTable` that can have a foreign key to either `foo` or `bar` but not to both.

**myTable**

```
+----+------+--------+--------+
| id | data | foo_id | bar_id |
+----+------+--------+--------+
|  1 | one  |  NULL  |   5    |
|  2 | two  |   7    |  NULL  |
+----+------+--------+--------+

```

When I insert into `myTable` it should have a foreign key in **either** foo\_id **or** bar\_id **but not** both... as shown in the table above.

Here are some of the queries I've tried to handle the **either/or** condition, but I can't seem to get it right. I've checked multiple threads and my queries seem to to match the answers there. `$my_string` is a session variable a use to keep track of which column to insert in... its values are either 'foo' or 'bar'

```
INSERT INTO myTable (data, if($my_string = foo,foo_id,bar_id)) values ('three',$id);

```

```
INSERT INTO myTable (data, (CASE WHEN $my_string IS foo THEN foo_id ELSE bar_id)) VALUES ('three',$id);
/*replacing the IS with an = doesn't change the outcome*/

```

I've also tried variations of adding quotes around the strings and removing them but they don't seem to change the outcome. I won't show all the variations to keep the question as short as possible

When I remove the condition part and only insert the `data` column the query functions properly... so I'm certain the condition is what's causing the issue.

**EDIT**

I'm **not** trying to add a constraint. I'm not afraid I'll add two values to `foo_id` and `bar_id`... I won't ever do that in any part of my application.

I have a two session variables `$id` and `$my_string`. I want an insert query that says: "If `$my_string` = 'foo' then insert `$id` into the column `foo_id`. Otherwise insert `$id` into `bar_id`".

I hope this clarifies the issue. Many thanks to those who answered already and helped clear this ambiguity.