Question

I have a following INSERT query and I am wondering whether it's possible to combine two columns with the same value? Yes, I must use exactly two of them for future altering.

insert into `products`
set
`name` = 'y',
`time` = 'x',
`time2` = 'x';

Thinking logically, pseudo-code time, time2 = 'x' should work somehow, but it doesn't. Thanks.

Était-ce utile?

La solution

The syntax you are using is not the standard syntax for insert. I believe it is special syntax for MySQL.

Here is one way to do this:

insert into `products`(name, time, time2)
    select 'y', 'x', 'x';

If you are doing this in MySQL and typing this out, you could do something like:

insert into `products`(name, time, time2)
    select 'y', val, val
    from (select 'x' as val) t;

Of course, in this case, that doesn't save any typing.

Autres conseils

No, you have to specify a value separately for each column.

The only suggestion I can offer for your case is to write a trigger to copy the value of the time column to time2. But it's probably not worth it to write a trigger for such a case unless you do it very frequently.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top