Check before to insert record into Database Table - mysql

I have a problem with inserting data into the database table using CRON script that is executing every day.
So, I am using that script to insert orders into order tables that have Order ID and Order number as keys.
But in the code, I am generating ID of order dynamically and article number is for each order incrementing for one.
But, with this solution, I cannot add check in my SQL query (IF NOT EXIST...), so orders will be duplicated all the time...And I don't have an idea at the moment for some smart solution...
Could someone provide me any suggestion for the problem?
Thank you.

I think you can use INSERT ... ON DUPLICATE KEY UPDATE Syntax.
Here you can find more information on how to use it.
Hope this will help you

Related

how can i put multiple records into 1 record in mysql

I was trying to move multiple records from one table to one record in another. with MYSQL,I tried Group_concat(), Concat() Group by, and Stored Procedure to breakdown the data and regroup. Unfortunately, my attempts have failed. I know you can help me because you are the experts. I thank you in advance. Please see image I attached.
Probably you require aggregation here, something like:
select Matrial, max(Qty_In_Jan) Qty_In_Jan, max(Qty_In_feb) Qty_In_feb....
from t
group by Matrial;

Insert random number into table upon new record creation

I would like to store random numbers in one MySql table, randomly retrieve one and insert it into another table column each time a new record is created. I want to delete the retrieved number from the random number table as it is used.
The random numbers are 3 digit, there are 900 of them.
I have read several posts here that describe the problems using unique random numbers and triggering their insertion. I want to use this method as it seems to be reliable while generating few problems.
Can anyone here give me an example of a sql query that will accomplish the above? (If sql query is not the recommended way to do this please feel free to recommend a better method.)
Thank you for any help you can give.
I put together the two suggestions here and tried this trigger and query:
CREATE TRIGGER rand_num before
INSERT ON uau3h_users FOR EACH ROW
insert into uau3h_users (member_number)
select random_number from uau3h_rand900
where random_number not in (select member_number from uau3h_users)
order by random_number
limit 1
But it seems that there is already a trigger attached to that table so the new one cause a conflict, things stopped working until I removed it. Any ideas about how accomplish the same using another method?
You are only dealing with 900 records, so performance is not a major issue.
If you are doing a single insert into a table, you can do something like the following:
insert into t(rand)
select rand
from rand900
where rand not in (select rand from t)
order by rand()
limit 1
In other words, you don't have to continually delete from one table and move to the other. You can just choose to insert values that don't already exist. If performance is a concern, then indexes will help in this case.
More than likely you need to take a look into Triggers. You can do some stuff for instance after inserting a record in a table. Refer this link to more details.
http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html

insert a row into a sql table from a number greater than 1 and increment all contents of column in following rows

I'm quite new to sql. I am using a mysql db with opensource cms. I want to insert a row into the zone table which has all of the locale names stored inside.
I want to insert a row at position 3561, and increment the value of zone id for all of the following rows. Can you help?
Also, if you know of any good tutorial resources that you could recommend and perhaps a decent online reference (both free please - I'm skint) then I'd be grateful.
Cheers
You don't want to do this. The zone_id should be an id in the zone table that serves no purpose other than identifying the row in the table. Generally, these are auto-incremented ids that simply add 1 to the previous largest id.
You can insert, delete, and modify the rows in the zone table. The id will always refer to the same row. This helps ensure relational integrity. So, you can refer to a row in the table using the id, rather than some other feature that might get updated.
If, for some reason, you need to output the rows in the table with a sequential id, there are ways to do this in most databases, including MySQL.
Thanks for your help. I received an answer from the opencart forum and it seems there is a php function to achieve this built into the admin.

storing records in mysql db in sorting order of date

I want to store some records in mysql database. records have a date column. i want to store them in sorting order of that date column.
For example, record having date 27/sep/2011 get stored as first row on the top of record having date 26/sep/2011 as:
id_1,name_1,27/sep/2011
id_2,name_2,26/sep/2011
if new records come on future dates they would get inserted on the top.
I DONT want to order them while using select by using order by desc .
i want they get inserted into db directly in sorted order.
how to do this???
thanks...
I am always surprised when people want to determine physical order of storing records.
Basically, it's a terrible idea for multiple reasons.
1) How the record is physically stored should not be of your concern.
2) How the record is presented should be of your concern. That's why we have ORDER BY built in.
3) Determining physical storage should be done by experts in the field, since it has performance implications - which is a topic in its own and I won't go into details.
Basically, worry about getting the data out in the sorted order, not getting it in in the sorted order.
Reason why it's a bad idea is because you'll be tampering with the primary key which is never, ever a good idea. On top of that, you'll have to reorder the records every time you insert something. Just don't reinvent hot water.
You could do this by adding another table - inserting all of the records into that table (the current and the new ones) then doing and insert as follows :
INSERT into newtable
select * from temptable
order by temptable.date
Why do you need to do this ? why not just use orderby on the query ?
As pointed out in the comments below - you would need to truncate the newtable each time
You cannot choose where to insert your row.
Here's one possible solution: MySQL syntax for inserting a new row in middle rows?

how can I add records through a query in access?

Before I remade this simple database, I was able to perform a query and update and insert new records from that query. I can no longer do this and can't figure out why.
I'm not very knowledgeable with access so any help would be greatly appreciated. I have a feeling that this may have something to do with the auto number column that no longer exists. Basically, it wouldn't allow me to change the data type after I had entered data into the table. I needed to do this to preserve the primary key as they match the client's records and cannot be changed.
If I understand you right you have a query that used to work (and insert records in a table) and now doewsn't work anymore? Can you post the SQL of the query and the structure of the table that you are inserting in? That will make it a lot easier for the rest of the world to help you.