Related
Previously, this was working:
$patient_story_set_photos = $wpdb->get_results('SELECT * FROM wp_before_after WHERE patientID = '.$post->ID.' AND patient_display = 1');
However, when I try to add another AND condition like this:
$patient_story_set_photos = $wpdb->get_results('SELECT * FROM wp_before_after WHERE patientID = '.$post->ID.' AND patient_display = 1 AND period_taken = '.$set->period_taken);
I get the following error on screen:
WordPress database error: [Unknown column '1hour' in 'where clause']
SELECT * FROM wp_before_after WHERE patientID = 8175 AND patient_display = 1 AND period_taken = 1hour
Can't see why there's a problem, are you not allowed to use multiple AND conditions in SQL?
The problem is not the AND, the problem is your 1hour, 1hour unquoted means a reference to an object (database, table) named 1hour, you need to quote '1hour'.
If you write
SELECT * FROM wp_before_after
WHERE patientID = 8175
AND patient_display = 1
AND period_taken = '1hour'
you will compare the field periodtaken to a string (CHAR,VARCHAR,TEXT) equal to '1hour'.
I assume period_taken is a field typed CHAR,VARCHAR or TEXT
Before anything, DO NOT CONCATENATE SQL STRINGS nowadays it is a MUST (see how to do it properly https://stackoverflow.com/a/60496/3771219)
The problem you are facing is because, I presume, that the period_taken field is some sort of Char/Varchar/String field and when you are filtering by a "Stringy" field you must sorround your literals values with single quotes:
SELECT *
FROM wp_before_after
WHERE patientID = 8175
AND patient_display = 1
AND period_taken = '1hour'
Hope this help
I would like to merge to tables in MySQL. In SQL I would use the 'MERGE' command, but what is the equivalent command in MySQL? Lets say i have 3 columns in both tables. Then i want to match the rows by the first column, and if there is a match it needs to update 2nd column but keep the original 3rd column and if there isnt a match then it needs to insert the new row.
Here is the SQL code I would like to convert to MySQL.
MERGE [Synsbasen].[dbo].[Koeretoej] AS T
USING [Synsbasen].[dbo].[KoeretoejLoad] AS S ON (T.KoeretoejIdent = S.KoeretoejIdent)
WHEN NOT MATCHED BY TARGET
THEN INSERT(KoeretoejIdent, KoeretoejArtNavn, KoeretoejAnvendelseNavn, RegistreringNummerNummer, KoeretoejOplysningStatus, KoeretoejOplysningFoersteRegistreringDato, KoeretoejOplysningStelNummer, KoeretoejMaerkeTypeNavn, KoeretoejModelTypeNavn, KoeretoejVariantTypeNavn, DrivkraftTypeNavn, SynResultatSynsType, SynResultatSynsDato, SynResultatSynStatusDato, SidsteSynTjek)
VALUES(S.KoeretoejIdent, S.KoeretoejArtNavn, S.KoeretoejAnvendelseNavn, S.RegistreringNummerNummer, S.KoeretoejOplysningStatus, S.KoeretoejOplysningFoersteRegistreringDato, S.KoeretoejOplysningStelNummer, S.KoeretoejMaerkeTypeNavn, S.KoeretoejModelTypeNavn, S.KoeretoejVariantTypeNavn, S.DrivkraftTypeNavn, S.SynResultatSynsType, S.SynResultatSynsDato, S.SynResultatSynStatusDato, CONVERT(VARCHAR(10),'1900-01-01',110))
WHEN MATCHED
THEN UPDATE SET
T.KoeretoejArtNavn = S.KoeretoejArtNavn,
T.KoeretoejAnvendelseNavn = S.KoeretoejAnvendelseNavn,
T.RegistreringNummerNummer = S.RegistreringNummerNummer,
T.KoeretoejOplysningStatus = S.KoeretoejOplysningStatus,
T.KoeretoejOplysningFoersteRegistreringDato = S.KoeretoejOplysningFoersteRegistreringDato,
T.KoeretoejOplysningStelNummer = S.KoeretoejOplysningStelNummer,
T.KoeretoejMaerkeTypeNavn = S.KoeretoejMaerkeTypeNavn,
T.KoeretoejModelTypeNavn = S.KoeretoejModelTypeNavn,
T.KoeretoejVariantTypeNavn = S.KoeretoejVariantTypeNavn,
T.DrivkraftTypeNavn = S.DrivkraftTypeNavn,
T.SynResultatSynsType = S.SynResultatSynsType,
T.SynResultatSynsDato = S.SynResultatSynsDato,
T.SynResultatSynStatusDato = S.SynResultatSynStatusDato;
Look at: https://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html
Your query should be something like this:
INSERT into Koeretoej
(KoeretoejIdent, KoeretoejArtNavn, KoeretoejAnvendelseNavn,
RegistreringNummerNummer, KoeretoejOplysningStatus,
KoeretoejOplysningFoersteRegistreringDato, KoeretoejOplysningStelNummer,
KoeretoejMaerkeTypeNavn, KoeretoejModelTypeNavn, KoeretoejVariantTypeNavn,
DrivkraftTypeNavn, SynResultatSynsType, SynResultatSynsDato,
SynResultatSynStatusDato, SidsteSynTjek)
SELECT
S.KoeretoejIdent, S.KoeretoejArtNavn, S.KoeretoejAnvendelseNavn,
S.RegistreringNummerNummer, S.KoeretoejOplysningStatus,
S.KoeretoejOplysningFoersteRegistreringDato,
S.KoeretoejOplysningStelNummer, S.KoeretoejMaerkeTypeNavn,
S.KoeretoejModelTypeNavn, S.KoeretoejVariantTypeNavn,
S.DrivkraftTypeNavn, S.SynResultatSynsType, S.SynResultatSynsDato,
S.SynResultatSynStatusDato, DATE_FORMAT("19000101","%m-%d-%Y")
FROM KoeretoejLoad S LEFT JOIN Koeretoej T ON
T.KoeretoejIdent = S.KoeretoejIdent
ON DUPLICATE KEY UPDATE
KoeretoejArtNavn=S.KoeretoejArtNavn,
KoeretoejAnvendelseNavn=S.KoeretoejAnvendelseNavn,
RegistreringNummerNummer=S.RegistreringNummerNummer,
KoeretoejOplysningStatus=S.KoeretoejOplysningStatus,
KoeretoejOplysningFoersteRegistreringDato=S.KoeretoejOplysningFoersteRegistreringDato,
KoeretoejOplysningStelNummer=S.KoeretoejOplysningStelNummer,
KoeretoejMaerkeTypeNavn=S.KoeretoejMaerkeTypeNavn,
KoeretoejModelTypeNavn=S.KoeretoejModelTypeNavn,
KoeretoejVariantTypeNavn=S.KoeretoejVariantTypeNavn,
DrivkraftTypeNavn=S.DrivkraftTypeNavn,
SynResultatSynsType=S.SynResultatSynsType,
SynResultatSynsDato=S.SynResultatSynsDato,
SynResultatSynStatusDato=S.SynResultatSynStatusDato,
SidsteSynTjek=DATE_FORMAT("19000101","%m-%d-%Y")
Presently troubleshooting a problem where running this SQL query:
UPDATE tblBenchmarkData
SET OriginalValue = DataValue, OriginalUnitID = DataUnitID,
DataValue = CAST(DataValue AS float) * 1.335
WHERE
FieldDataSetID = '6956beeb-a1e7-47f2-96db-0044746ad6d5'
AND ZEGCodeID IN
(SELECT ZEGCodeID FROM tblZEGCode
WHERE(ZEGCode = 'C004') OR
(LEFT(ZEGParentCode, 4) = 'C004'))
Results in the following error:
Msg 8114, Level 16, State 5, Line 1
Error converting data type nvarchar to float.
The really odd thing is, if I change the UPDATE to SELECT to inspect the values that are retrieved are numerical values:
SELECT DataValue
FROM tblBenchmarkData
WHERE FieldDataSetID = '6956beeb-a1e7-47f2-96db-0044746ad6d5'
AND ZEGCodeID IN
(SELECT ZEGCodeID
FROM tblZEGCode WHERE(ZEGCode = 'C004') OR
(LEFT(ZEGParentCode, 4) = 'C004'))
Here are the results:
DataValue
2285260
1205310
Would like to use TRY_PARSE or something like that; however, we are running on SQL Server 2008 rather than SQL Server 2012. Does anyone have any suggestions? TIA.
It would be helpful to see the schema definition of tblBenchmarkData, but you could try using ISNUMERIC in your query. Something like:
SET DataValue = CASE WHEN ISNUMERIC(DataValue)=1 THEN CAST(DataValue AS float) * 1.335
ELSE 0 END
Order of execution not always matches one's expectations.
If you set a where clause, it generally does not mean the calculations in the select list will only be applied to the rows that match that where. SQL Server may easily decide to do a bulk calculation and then filter out unwanted rows.
That said, you can easily write try_parse yourself:
create function dbo.try_parse(#v nvarchar(30))
returns float
with schemabinding, returns null on null input
as
begin
if isnumeric(#v) = 1
return cast(#v as float);
return null;
end;
So starting with your update query that's giving an error (please forgive me for rewriting it for my own clarity):
UPDATE B
SET
OriginalValue = DataValue,
OriginalUnitID = DataUnitID,
DataValue = CAST(DataValue AS float) * 1.335
FROM
dbo.tblBenchmarkData B
INNER JOIN dbo.tblZEGCode Z
ON B.ZEGCodeID = Z.ZEGCodeID
WHERE
B.FieldDataSetID = '6956beeb-a1e7-47f2-96db-0044746ad6d5'
AND (
Z.ZEGCode = 'C004' OR
Z.ZEGParentCode LIKE 'C004%'
)
I think you'll find that a SELECT statement with exactly the same expressions will give the same error:
SELECT
OriginalValue,
DataValue NewOriginalValue,
OriginalUnitID,
DataUnitID OriginalUnitID,
DataValue,
CAST(DataValue AS float) * 1.335 NewDataValue
FROM
dbo.tblBenchmarkData B
INNER JOIN dbo.tblZEGCode Z
ON B.ZEGCodeID = Z.ZEGCodeID
WHERE
B.FieldDataSetID = '6956beeb-a1e7-47f2-96db-0044746ad6d5'
AND (
Z.ZEGCode = 'C004' OR
Z.ZEGParentCode LIKE 'C004%'
)
This should show you the rows that can't convert:
SELECT
B.*
FROM
dbo.tblBenchmarkData B
INNER JOIN dbo.tblZEGCode Z
ON B.ZEGCodeID = Z.ZEGCodeID
WHERE
B.FieldDataSetID = '6956beeb-a1e7-47f2-96db-0044746ad6d5'
AND (
Z.ZEGCode = 'C004' OR
Z.ZEGParentCode LIKE 'C004%'
)
AND IsNumeric(DataValue) = 0
-- AND IsNumeric(DataValue + 'E0') = 0 -- try this if the prior doesn't work
The trick in the last commented line is to tack on things to the string to force only valid numbers to be numeric. For example, if you wanted only integers, IsNumeric(DataValue + '.0E0') = 0 would show you those that aren't.
i wrote a command like this to update a column in one table with avg of columns from another table.. its giving errors
UPDATE college_rating,products set
property1_avg = avg(college_rating.rating1),
property2_avg = avg(college_rating.rating2),
property3_avg = avg(college_rating.rating3),
property4_avg = avg(college_rating.rating4),
property5_avg = avg(college_rating.rating5),
property6_avg = avg(college_rating.rating6),
property7_avg = avg(college_rating.rating7),
property8_avg = avg(college_rating.rating8),
property9_avg = avg(college_rating.rating9),
property10_avg = avg(college_rating.rating10),
property11_avg = avg(college_rating.rating11),
property12_avg = avg(college_rating.rating12),
property13_avg = avg(college_rating.rating13),
property14_avg = avg(college_rating.rating14),
property15_avg = avg(college_rating.rating15)
where products.alias = concat(college_rating.property1,'-',college_rating.property2,'-',college_rating.property3)
group by college_rating.property1,college_rating.property2, college_rating.property3
The MySQL multi-table update syntax does not allow the use of group by.
You can accomplish what you are trying to do by moving the aggregation into a sub-query and joining to that sub-query in the multi-table-update instead.
Something like this should work:
update products p
inner join (
select concat(property1,'-',property2,'-',property3) as alias,
avg(rating1) as property1_avg,
avg(rating2) as property2_avg,
avg(rating3) as property3_avg,
avg(rating4) as property4_avg,
avg(rating5) as property5_avg,
avg(rating6) as property6_avg,
avg(rating7) as property7_avg,
avg(rating8) as property8_avg,
avg(rating9) as property9_avg,
avg(rating10) as property10_avg,
avg(rating11) as property11_avg,
avg(rating12) as property12_avg,
avg(rating13) as property13_avg,
avg(rating14) as property14_avg,
avg(rating15) as property15_avg
from college_rating
group by property1,property2, property3
) as r on r.alias = p.alias
set p.property1_avg = r.property1_avg,
p.property2_avg = r.property2_avg,
p.property3_avg = r.property3_avg,
p.property4_avg = r.property4_avg,
p.property5_avg = r.property5_avg,
p.property6_avg = r.property6_avg,
p.property7_avg = r.property7_avg,
p.property8_avg = r.property8_avg,
p.property9_avg = r.property9_avg,
p.property10_avg = r.property10_avg,
p.property11_avg = r.property11_avg,
p.property12_avg = r.property12_avg,
p.property13_avg = r.property13_avg,
p.property14_avg = r.property14_avg,
p.property15_avg = r.property15_avg;
What is the error that you get? And you need to have a WHERE clause unless you want the UPDATE query to apply to ALL the records
I think you'd need to use sub queries, and I'm not sure if you can update two tables like that in MySQL, at least not without prefixing the attributes.
I have an update statement that updates fields x, y and z where id = xx.
In the table I have a few different x_created_datetime fields (for different portions of the record that are maintained/entered by different folks). I'd like to write a single query that will update this field if is null, but leave it alone if is not null.
So what I have is:
UPDATE newspapers
SET scan_notes = "data",
scan_entered_by = "some_name",
scan_modified_date = "current_unix_timestamp"
WHERE id = X
What I need is a way to add in the following, but still always update the above:
scan_created_date = "current_unix_timestamp"
where scan_created_date is null
I'm hoping I can do this without a second transaction to the DB. Any ideas on how to accomplish this?
Do this:
UPDATE newspapers
SET scan_notes = "data",
scan_entered_by = "some_name",
scan_modified_date = "current_unix_timestamp",
scan_created_date = COALESCE(scan_created_date, "current_unix_timestamp")
WHERE id = X
The COALESCE function picks the first non-null value. In this case, it will update the datestamp scan_created_date to be the same value if it exists, else it will take whatever you replace "current_unix_timestamp" with.
mySQL has an IFNULL function, so you could do:
UPDATE newspapers
SET scan_notes = "data",
scan_entered_by = "some_name",
scan_modified_date = "current_unix_timestamp"
scan_created_date = IFNULL( scan_created_date, "current_unix_timestamp" )
WHERE id = X
I think that what you're looking for is IF()
UPDATE newspapers
SET scan_notes = "data",
scan_entered_by = "some_name",
scan_modified_date = "current_unix_timestamp",
scan_created_date = IF(scan_created_date IS NOT NULL, "current_unix_timestamp", NULL)
WHERE id = X
You could use COALESCE() wich returns the first NON-NULL value):
scan_created_date = COALESCE(scan_created_date, "current_unix_timestamp")
You can do something like this:
UPDATE newspapers a, newspapers b
SET a.scan_notes = "data",
a.scan_entered_by = "some_name",
a.scan_modified_date = "current_unix_timestamp",
b.scan_created_date = "current_unix_timestamp"
WHERE a.id = X AND b.id = X AND b.scan_created_date is not NULL
Its like equivalent to Oracle's NVL.
You can use it like below in a prepared statement using parameters
UPDATE
tbl_cccustomerinfo
SET
customerAddress = COALESCE(?,customerAddress),
customerName = COALESCE(?,customerName),
description = COALESCE(?,description)
WHERE
contactNumber=?