In an Access query, if I want to compare 2 fields, I do something like :-
IIf([Field1] = [Field2], "Yes", "No") AS [MATCH]
Is it possible to compare 3 fields, and if all the 3 values are different then result should be Yes, otherwise the result should be No.
If any 2 of the 3 fields are equal, you want the result to be "No".
So use the OR operator:
IIf(
[Field1] = [Field2] OR [Field1] = [Field3] OR [Field2] = [Field3],
"No",
"Yes"
) AS [MATCH]
GoodJuJus answer is almost there.
IIf(([Field1]=[Field2] AND [Field2]=[Field3] AND [Field1]=[Field3]),"No","Yes") AS [MATCH]
This will allow only cases where all three fields are different.
If you want to accept cases with 2 (but not 3) identical values this will not work.
Anyway inverting the logic might make it nicer to read:
IIf(([Field1]<>[Field2] And [Field2]<>[Field3] And [Field1]<>[Field3]),"Yes","No") AS [MATCH]
Here are 2 ways to do it:
In my examples, I have it set to "yes" if there's a match, so you may have to tweak it. I'm unsure if you really want it to say "yes" if it's not a match.
Using the Macro Builder
In case you want to use VBA:
Private Sub Command20_Click()
If Text1.Value = Text2.Value Then
step1 = "yes"
End If
If Text1.Value <> Text2.Value Then
step1 = "no"
End If
If Text1.Value = Text3.Value Then
step2 = "yes"
End If
If Text1.Value <> Text3.Value Then
step1 = "no"
End If
If step1 = step2 Then
Label25.Caption = "yes"
End If
If step1 <> step2 Then
Label25.Caption = "no"
End If
Yes, you can just expand your logic with the logical AND operator:
IIf(([Field1]=[Field2] And [Field2]=[Field3]),"Yes","No") AS [MATCH]
Note though that in your question you said, 'if all the 3 values are different then result should be Yes, otherwise the result should be No'. This contradicts your SQL query that has 'Yes' where the values are equal. Switch your "Y","N" around to suit....
This will return:
|-------|-------|-------|-------|
|Field1 |Field2 |Field3 |Result |
|-------|-------|-------|-------|
|1234 |1234 |1234 |Y |
|1234 |aabb |aabb |N |
|1234 |we |1234 |N |
|-------------------------------|
Related
I would like to do the following.
Update a field based on the value of another field like
update table set if(fielda=1){fieldb=2 fieldc=3}else{fieldd=2 fielde=3}
I know this is not valid mysql but its the best way for me to describe the problem.
update table set
b = case when a = 1 then 2 else b end,
c = case when a = 1 then 3 else c end,
d = case when a = 1 then d else 2 end,
e = case when a = 1 then e else 3 end
edit
according to your comment try this:
update table set
datefield_a = case when field_a = 1 then now() else datefield_a end,
datefield_b = case when field_a <> 1 then now() else datefield_b end
I think this syntax will achieve the result you attempted to specify.
UPDATE mytable
SET fieldb = CASE WHEN fielda = 1 THEN 2 ELSE fieldb END
, fieldc = CASE WHEN fielda = 1 THEN 3 ELSE fieldc END
, fieldd = CASE WHEN fielda = 1 THEN fieldd ELSE 2 END
, fielde = CASE WHEN fielda = 1 THEN fielde ELSE 3 END
The "trick" here is that we are updating all four columns, but in some "cases", we are assigning the current value of the column back to the column, resulting in no real change to the column value. (Once you get your mind bent around that idea, it's pretty easy.)
With MySQL, we do have a handy IF function (not available in most other RDBMS) that we can use to abbreviate that a bit, and achieve the same thing:
UPDATE mytable
SET fieldb = IF(fielda = 1, 2, fieldb)
, fieldc = IF(fielda = 1, 3, fieldc)
, fieldd = IF(fielda = 1, fieldd, 2)
, fielde = IF(fielda = 1, fielde, 3)
The pain is that you still have to repeat that same conditional test multiple times.
A single scan through the table (like these statements do), and getting all those assignments done in one fell swoop is going to be faster (and more efficient) than breaking this up and doing the assignments piecemeal using multiple statements.
I have a table with a few thousand entries. And My purpose is to select all entries from all versions that correspond to a given one. And the resulted entries must correspond exactly to the given entry.
But somehow, the SQL query does not work. The original project uses Access 2007. But I have tried also in MySQL and no success
I put here the sql query, but I also made a SQL fiddle:
SELECT
idvalue,
idtag,
iddevice,
idversion,
idtext,
description,
idaccess,
defaultvalue,
minimumvalue,
acceptedvalue,
maximumvalue,
outofrangevalue,
iddatatypepn,
iddatatypeopc,
size,
idresolution,
idunit,
idaccuracy,
enumerationvalues,
comments,
remanentvolatile,
storedatpn,
storedatmain,
`generated`,
edittime
FROM
SomeValues
WHERE
idtag = 2 AND iddevice = 1
AND idtext = 433
AND description = 'Input voltage (AC)'
AND idaccess = 12
AND defaultvalue IS NULL
AND minimumvalue =0
AND acceptedvalue = 5300
AND maximumvalue = 10050
AND outofrangevalue = 11000
AND iddatatypepn = 2
AND iddatatypeopc = 19
AND size = 2
AND idresolution = 2
AND idunit = 1
AND idaccuracy = 2
AND enumerationvalues IS NULL
AND comments IS NULL
AND remanentvolatile IS NULL
AND storedatpn = FALSE
AND storedatmain = FALSE
AND `generated` = TRUE
Fiddle: here
Can you please explain what is wrong with the sql query?
The result should be those 3 entries from the fiddle table.
And yes, I must use all the conditions from the "Where" clause, since the entries can match 90% but also have small differences
You have problem in line:
AND description = 'Input voltage (AC)'
change it to:
AND description = '"Input voltage (AC)"'
and everything will works.
Problem lies in the fact that you searched for text Input voltage (AC) instead of "Input voltage (AC)" (how is stated in column description).
Table invitation (Primary key: invitation_id)
---------------------------------------------
invitation_id send_user_id receive_user_id
---------------------------------------------
1 2 1
2 3 2
3 2 7
4 9 2
---------------------------------------------
If send_user_id value is 2 then I need to get receive_user_id and if receive_user_id is 2 then I need to get send_user_id
How to get this ?
Use a CASE expression:
SELECT invitation_id,
CASE WHEN send_user_id = 2 THEN receive_user_id
WHEN receive_user_id = 2 THEN send_user_id
ELSE -1 END AS output
FROM invitation
Note that I am returning -1 should neither of the two conditions be true, but we can easily replace this with other logic if you provide it.
try using nested if else if you have multiple conditions to check
if // your condition
begin
//enter you code
end
else if //2nd condition
begin
// your code
end
else //default condition if none of the above execute
begin
//default code
end
SELECT invitation_id,
CASE
WHEN receive_user_id = 2 THEN send_user_id
WHEN send_user_id = 2 THEN receive_user_id
END AS output
FROM INVITATION.
May be its give proper answer.
Format code properly.
I'm trying to migrate some data from an old database to a new one with a slightly different schema and my SQL isn't terribly strong.
Old schema: There is a table we'll call "Person" with a field which can have set of permutations of 3 flags. The Person table has a foreign key to another table we'll call "Flags". They Flags table has rows for each of these flag combinations in a String:
1 - Yes No No
2 - Yes Yes No
3 - Yes No Yes
4 - Yes Yes Yes
5 - No Yes No
6 - No Yes Yes
7 - No No Yes
The new schema doesn't require this table (thankfully). These flags are simply fields in the "Person" table now as BIT fields.
What I want to do is something like a:
UPDATE database2.Person SET (flag1, flag2, flag3) VALUES (true, false false) WHERE database1.Person.flag_id = 1;
I could then run 7 different queries changing the IDs and the values accordingly. The problem, of course, is that the above isn't correct SQL. I think I need some kind of JOIN ...or a subselect in the where clause or something?
Stumped on the best way forward. My parting thought here is that this doesn't need to be compressed into a single query, or particularly elegant. I expect to run this query once and be done with it.
You could try something like:
update database2.Person p2 join database1.Person p1 on p1.PersonId = p2.PersonId
set flag1 = case when p1.Flag_id in (1,2,3,4) then true else false end case,
flag2 = case when p1.Flag_id in (2,4,5,6) then true else false end case,
flag3 = case when p1.Flag_id in (3,4,6,7) then true else false end case
(edited for mySQl syntax)
This should work if I'm understanding your question. It assumes you have a Person_Id matching in each table.
UPDATE db2.Person p
JOIN db1.Person p2 ON p.Person_Id = p2.Person_Id
SET p.Flag1 = 1,
p.Flag2 = 0,
p.Flag3 = 0
WHERE p2.Flag_Id = 1;
If the flag values in the Flags table were columns, you could easily run a single query, but as I'm understanding it, it's just a string field. This is an example:
UPDATE db2.Person p
JOIN db1.Person p2 ON p.Person_Id = p2.Person_Id
JOIN db1.Flags f ON p2.Flag_Id = f.Flag_Id
SET p.Flag1 = CASE WHEN f.Flag1 = 'Yes' THEN 1 ELSE 0 END,
p.Flag2 = CASE WHEN f.Flag2 = 'Yes' THEN 1 ELSE 0 END,
p.Flag3 = CASE WHEN f.Flag3 = 'Yes' THEN 1 ELSE 0 END
In my database (MySQL) table, has a column with 1 and 0 for represent true and false respectively.
But in SELECT, I need it replace for true or false for printing in a GridView.
How to I make my SELECT query to do this?
In my current table:
id | name | hide
1 | Paul | 1
2 | John | 0
3 | Jessica | 1
I need it show thereby:
id | name | hide
1 | Paul | true
2 | John | false
3 | Jessica | true
You have a number of choices:
Join with a domain table with TRUE, FALSE Boolean value.
Use (as pointed in this answer)
SELECT CASE WHEN hide = 0 THEN FALSE ELSE TRUE END FROM
Or if Boolean is not supported:
SELECT CASE WHEN hide = 0 THEN 'false' ELSE 'true' END FROM
I got the solution
SELECT
CASE status
WHEN 'VS' THEN 'validated by subsidiary'
WHEN 'NA' THEN 'not acceptable'
WHEN 'D' THEN 'delisted'
ELSE 'validated'
END AS STATUS
FROM SUPP_STATUS
This is using the CASE
This is another to manipulate the selected value for more that two options.
You can do something like this:
SELECT id,name, REPLACE(REPLACE(hide,0,"false"),1,"true") AS hide FROM your-table
Hope this can help you.
If you want the column as string values, then:
SELECT id, name, CASE WHEN hide = 0 THEN 'false' ELSE 'true' END AS hide
FROM anonymous_table
If the DBMS supports BOOLEAN, you can use instead:
SELECT id, name, CASE WHEN hide = 0 THEN false ELSE true END AS hide
FROM anonymous_table
That's the same except that the quotes around the names false and true were removed.
You can use casting in the select clause like:
SELECT id, name, CAST(hide AS BOOLEAN) FROM table_name;
I saying that the case statement is wrong but this can be a good solution instead.
If you choose to use the CASE statement, you have to make sure that at least one of the CASE condition is matched. Otherwise, you need to define an error handler to catch the error. Recall that you don’t have to do this with the IF statement.
SELECT if(hide = 0,FALSE,TRUE) col FROM tbl; #for BOOLEAN Value return
or
SELECT if(hide = 0,'FALSE','TRUE') col FROM tbl; #for string Value return
in Postgres 11 I had to do this:
type is an int
SELECT type,
CASE
WHEN type = 1 THEN 'todo'
ELSE 'event'
END as type_s
from calendar_items;
replace the value in select statement itself
(CASE WHEN Mobile LIKE '966%' THEN (select REPLACE(CAST(Mobile AS nvarchar(MAX)),'966','0')) ELSE Mobile END)