将值与数据库中的两列进行比较? [英] compare value with two columns in database ?

查看:453
本文介绍了将值与数据库中的两列进行比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI,

我想比较数据库中两列的值。



就像我有一个名为vehicleid的值。在插入dataabse之前,我想将其与两列vehicleidin / vehicleidout进行比较。如果这两列的任何行都不存在该值,并且只允许将其保存到数据库。





我知道我们可以写一些像




i want to compare a value for two columns in the database.

like i have a value called vehicleid. before insert into dataabse i want to compare that with two columns vehicleidin / vehicleidout. if that value is not present with any of the row for these two columns than and only it will allow to save this to database.


for one column i know we can write something like

if vehicleid not in (vehicleidin) than 
begin
insert into ...
end 





但不知道两列的内容。



but dont know what to with two columns .

推荐答案


试试看....



Hi Try it out....

Insert into orders (orders_id, customers_id, customers_cid, customers_vat_id, customers_name, customers_email_address)
select
    o.*
from
    Test.dbo.orders o
where
    not exists (
        select 1
        from
            CobraDemoData.dbo.Data a
        where
            a.email0 = o.customers_email_address
    )







另外,您可能希望在select语句中指定列,以确保右列被转置。




Also, you probably want to specify the columns in the select statement, just to make sure the right columns are transposed.


这篇关于将值与数据库中的两列进行比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆