Trying to insert single line into DB:
INSERT INTO ok_users
( email
, password
, name
, phone
, address
, group_id
, last_ip
, created
, remind_code
, remind_expire) VALUES
( "test#test.com"
, "asdasdsasda"
, "szusalmeitiwzchGP zwusalmeznqzdqbGP"
, "000"
, "test, "
, "30"
, "127.0.0.1"
, "2019-03-31 03:00:00"
, null
, null)
and receives following error:
1292 - Incorrect datetime value: '2019-03-31 03:00:00' for column 'created' at row 1, Time: 0.000000s
If I change date to 2018-03-30 03:00:00 it will work.
Could anyone help me understand why?
Thanks!
I have this query:
SELECT id
, order_date
, seat_number
, cashier_fk
, branch_fk
, waiter_fk
, void
, total_amount
, customer_name
, payment
, notes
, down_payment
, received_date
, void_reason
, discount
, discount_percentage
, printed
, done
, vat
, service_charge
FROM order_tbl
WHERE received_date between "2018-03-15" AND "2018-03-18"
but it shows
My table has this record:
id-------order_date
1-------2018-03-09 09:09:25
2-------2018-03-13 18:29:16
3-------2018-03-13 20:00:49
4-------2018-03-13 20:01:46
5-------2018-03-13 20:05:48
6-------2018-03-13 20:06:34
7-------2018-03-13 20:07:15
9-------2018-03-16 19:06:23
10-------2018-03-16 20:22:26
But it shows only
id-------order_date
5-------2018-03-13 20:05:48
6-------2018-03-13 20:06:34
7-------2018-03-13 20:07:15
9-------2018-03-16 19:06:23
10-------2018-03-16 20:22:26
What is wrong with my query?
Do not use between with dates or date/times. The time component throws everything off.
Instead, express the logic as:
WHERE received_date >= '2018-03-15' AND
received_date < '2018-03-19'
Note the inequality at the end of the range. This ensures that you get everything from that date.
Aaron Bertrand has a really good blog post on the subject, What do BETWEEN and the devil have in common?.
We are trying to build a de-normalised version of our database for use in Tableau. One of the challenges we have is dealing with our key/value pairs in one of our tables. See the following simplified version of some data.
Asset Table
Asset ID Site ID Make Model
1 1 Toyota Corolla
2 2 Honda Civic
3 2 Suzuki Swift
Asset Property Types Table
Site ID Asset Property Name Asset Property Type
1 Odometer Numeric
1 Registration Text
1 Expiry Date Date
2 Odometer Numeric
2 Registration Text
2 Expiry Date Date
2 Colour Text
Asset Properties Table
Asset ID Key Text Value Numeric Value Date Value
1 Odometer 1234
1 Registration ABC123
1 Expiry Date 2018-02-08
2 Odometer 1255
2 Registration ABC124
2 Colour Red
2 Expiry Date 2018-01-08
3 Registration ABC125
3 Odometer 1266
3 Colour Blue
3 Expiry Date 2018-03-25
Some points to note on this. This is a simplified version of the data. Not all Assets will have the same key/value pairs. Sites will have individual data types that they want to store which may be different to other sites.
Ultimately here is what we want to try and achieve:
Site 1 Asset Table
Asset ID Make Model Odometer Registration Expiry Date
1 Toyota Corolla 1234 ABC123 2018-02-08
Site 2 Asset Table
Asset ID Make Model Odometer Registration Expiry Date Colour
2 Honda Civic 1255 ABC124 2018-01-08 Red
3 Suzuki Swift 1266 ABC125 2018-03-25 Blue
My general approach to this was the following:
Create the Site Specific Asset Tables with just the Make & Model and populate them.
Use a loop to ALTER the table adding a column for each of the Asset Properties that exist
Populate the Site Specific Asset table with the Asset Property data in the appropriate columns
I was hoping to do all of this with a MySQL Procedure so that I can schedule it to run automatically every hour and replace the various Site level tables. A CASE statement will not work as this needs to be dynamic.
I really appreciate any advice/help on how to achieve this. Whilst I'm okay with SQL, procedures are way out of my depth.
I guess the difficult bit is building dynamic case statements. So given your data (with some changes to remove white spaces and possible key word clashes)
create table Asset(Asset_ID int, Site_ID int, Make varchar(20), Model varchar(20));
insert into asset values
(1 , 1 , 'Toyota' , 'Corolla'),
(2 , 2 , 'Honda' , 'Civic'),
(3 , 2 , 'Suzuki' , 'Swift');
drop table if exists Asset_Property_Types;
create table Asset_Property_Types (Site_ID int, Asset_Property_Name varchar(100), Asset_Property_Type varchar(100));
insert into asset_property_types values
(1 , 'Odometer' , 'Numeric_value'),
(1 , 'Registration' , 'Text_value'),
(1 , 'Expiry_Date' , 'Date_value'),
(2 , 'Odometer' , 'Numeric_value'),
(2 , 'Registration' , 'Text_value'),
(2 , 'Expiry_Date' , 'Date_value'),
(2 , 'Colour' , 'Text_value');
create table Asset_Properties(Asset_ID int , asset_property_name varchar(30),Text_Value varchar(100), Numeric_Value int , Date_Value date);
insert into asset_properties values
(1 , 'Odometer' , null , 1234 ,null),
(1 , 'Registration' , 'ABC123' ,null ,null),
(1 , 'Expiry_Date' , null , null, '2018-02-08'),
(2 , 'Odometer' , null , 1255 ,null),
(2 , 'Registration' , 'ABC124' ,null ,null),
(2 , 'Colour' , 'Red' ,null ,null),
(2 , 'Expiry_Date' , null , null ,'2018-01-08'),
(3 , 'Registration' , 'ABC125' ,null ,null),
(3 , 'Odometer' , null , 1266,null),
(3 , 'Colour' , 'Blue' ,null ,null),
(3 , 'Expiry_Date' , null , null ,'2018-03-25');
This code
set #sql = (select concat('select a.asset_id ,a.site_id,a.make,a.model,',
group_concat(
concat('max(case when asset_property_name = ' ,char(39),asset_property_name,char(39), ' then ' ,asset_property_type ,' else null end) as ', asset_property_name)
)
,' from asset a join asset_properties ap on ap.asset_id = a.asset_id group by a.site_id,a.asset_id;'
)
from
(select distinct asset_property_name,asset_property_type from asset_property_types) a
)
;
builds this sql statement
select a.asset_id ,a.site_id,a.make,a.model,
max(case when asset_property_name = 'Odometer' then Numeric_value else null end) as Odometer,
max(case when asset_property_name = 'Registration' then Text_value else null end) as Registration,
max(case when asset_property_name = 'Expiry_Date' then Date_value else null end) as Expiry_Date,
max(case when asset_property_name = 'Colour' then Text_value else null end) as Colour
from asset a join asset_properties ap on ap.asset_id = a.asset_id
group by a.site_id,a.asset_id;
Which you can then submit to sql like so
prepare sqlstmt from #sql;
execute sqlstmt;
deallocate prepare sqlstmt;
Your problem is similar to the pivot table problem. You can google it to find further variants of solving it. One way to solve it is
A) Fetch the column names
`Select distinct key from assetPropertiesTable;`
B) Build a Select query string using dynamic SQL that, for each key k from the previous query, has a column using a subselect like
(select textvalue from assetPropertiesTable t where t.id = outerselect.id and key = k) as column_k
C) Execute the full query and either return it from the stored procedure or do a create table xyz as /*select query here*/.
No need to create and fill the table in two steps.
There are more distinguished and more performant ways of doing that, for example joining the assetPropertiesTable once and doing sums on boolean condition key=k for each column you want to add
I am new to mysql db programming, i am not able to figure out what is the error in the mysql insert statement i am executing. MySql Query browser is giving error: error in your SQL syntax.
Structure of my database table:
Command:
insert into mail_client.inbox ( to , from , subject , date , message , uuid )
values ( "fdfgmail.com" , "fromgmail.com" , "subj" , "2016-06-06" , "hello" ,
"0757c2666e934e2eb303df68bb3c9722" );
Add backticks to the reserved names fields:
`insert into mail_client.inbox ( `to` , `from` , subject , `date` , message , uuid ) values ( "fdfgmail.com" , "fromgmail.com" , "subj" , "2016-06-06" , "hello" , "0757c2666e934e2eb303df68bb3c9722" );`
HI i have two tables in my database named...Requests and Balance tracker which has no relation....but i want to select data from two tables and binf it two grid...
Requests
EmpID |EmpRqsts|EmpDescription|ApproverID|ApprovedAmount|RequestPriority
1 |asdfsb |sadbfsbdf |1 |
2 |asbfd |sjkfbsd |1 |
Balance Tracker
EmpId|BalanceAmnt|LastUpdated|lastApprovedAmount
| 1 |5000 |sdfbk |
| 2 |3000 |sjbfsh |
now i want to update based on the EmpID two tables at a time...when ever amount is approved it should be updates in request table column [ApprovedAmount] and with priority...
when [ApprovedAmount] is Updated [BalanceAmnt] Balance Tracker of also should be Updated by adding the amount approved,[LastUpdated],[lastApprovedAmount] should be updated with date and time
can any one help me with the query please....
#Anil, here is an example of SQL Server 2008 code which would help you to get your goal acomplished:
DECLARE #Requests TABLE
(
EmpId int
, EmpRqsts nvarchar(50)
, EmpDescription nvarchar(250)
, ApproverID int
, ApprovedAmount money
, RequestPriority int
)
DECLARE #BalanceTracker TABLE
(
EmpId int
, BalanceAmnt money
, LastUpdated datetime
, lastApprovedAmount money
)
-- Insert data for testing
INSERT INTO #Requests VALUES
(
1
, 'Something here'
, 'Some descriptio here'
, 1
, 100
, 1
)
INSERT INTO #Requests VALUES
(
2
, 'Something here 2 '
, 'Some descriptio here 3'
, 1
, 215
, 2
)
INSERT INTO #BalanceTracker VALUES
(
1
, 5000
, GETDATE() - 3
, 310
)
INSERT INTO #BalanceTracker VALUES
(
2
, 3000
, (GETDATE() - 1)
, 98
)
-- Declare local variables
DECLARE
#NewAmount money
, #NewPriority int
, #SelectedEmpId int
-- Assing values for example
SELECT #NewAmount = 1000
, #SelectedEmpId = 1
, #NewPriority = 5
-- Get the tables values pre - updates
SELECT *
FROM #Requests
SELECT *
FROM #BalanceTracker
BEGIN TRY
-- Update the record with new ApprovedAmount and Request Priority
UPDATE #Requests
SET ApprovedAmount = #NewAmount
, RequestPriority = #NewPriority
WHERE EmpId = #SelectedEmpId
-- If no error found then update BalanceAmnt trable
IF (##ERROR = 0)
BEGIN TRY
UPDATE #BalanceTracker
SET BalanceAmnt = (BalanceAmnt + #NewAmount)
, LastUpdated = GETDATE()
, lastApprovedAmount = #NewAmount
WHERE EmpId = #SelectedEmpId
END TRY
BEGIN CATCH
PRINT N'Error found updating #BalanceTracker table: ' + ISNULL(LTRIM(STR(ERROR_NUMBER())) , N'Unknown Error' )
+ N', Message: ' + ISNULL ( ERROR_MESSAGE() , N'No Message' )
END CATCH
END TRY
BEGIN CATCH
PRINT N'Error found updating #Requests table: ' + ISNULL(LTRIM(STR(ERROR_NUMBER())) , N'Unknown Error' )
+ N', Message: ' + ISNULL ( ERROR_MESSAGE() , N'No Message' )
END CATCH
-- Get the tables values post - updates
SELECT *
FROM #Requests
SELECT *
FROM #BalanceTracker
Note 1: #Table are Variable Tables handlded by SQL Server 2008. If you're using previous version you should be able to create Temporary Table (#Table).
Note 2: data data-types may vary depending upon the SQL version you're using.
You could do this type of thing with a trigger. This way whenever you do the first update, it will automatically do the other update you specify.