I have a table in mysql with the following headings:
staff_id,dept_id,role_id,username,firstname,lastname,passwd,backend,email,phone,phone_ext,mobile,signature,lang,timezone,locale,notes,isactive,isadmin,isvisible,onvacation,assigned_only,show_assigned_tickets,change_passwd,max_page_size,auto_refresh_rate,default_signature_type,default_paper_size,extra,permissions,created,lastlogin,passwdreset,updated
staff_id is a primary key value and is set to AUTO_INCREMENT.
I found the solution by Queue in this post really helpful, although when the data is being imported the staff_id column is not being auto-incremented. I am inserting the column names in the Format-Specific Options in phpmysql. I can only get it to populate if the staff_id value exists in the csv file I am trying to import.
So if the data looks like this:
2,1,1,agent,Mista,Busta,NULL,NULL,agent#company.org,,NULL,,,NULL,NULL,NULL,<p>this is an agent; mista busta; agent#company.org</p>,1,0,1,0,0,0,0,0,0,none,Letter,"{""def_assn_role"":true}","{""user.create"":1,""user.delete"":1,""user.edit"":1,""user.manage"":1,""user.dir"":1,""org.create"":1,""org.delete"":1,""org.edit"":1,""faq.manage"":1}",2020-02-04 10:18:42,NULL,NULL,2020-02-04 10:18:42
...note the first '2' is the staff_id. What I would like do is have this in the csv:
,1,1,agent,Mista,Busta,NULL,NULL,agent#company.org,,NULL,,,NULL,NULL,NULL,<p>this is an agent; mista busta; agent#company.org</p>,1,0,1,0,0,0,0,0,0,none,Letter,"{""def_assn_role"":true}","{""user.create"":1,""user.delete"":1,""user.edit"":1,""user.manage"":1,""user.dir"":1,""org.create"":1,""org.delete"":1,""org.edit"":1,""faq.manage"":1}",2020-02-04 10:18:42,NULL,NULL,2020-02-04 10:18:42
...leaving the staff_id column blank and allowing mysql to auto-populate (auto_increment) as it sees fit.
AUTO INCREMENT is set at the server level within MySQL, and can be overwritten if assigned manually within a CSV or other import. If you simply exclude the entire column from your import, you'll allow MySQL to do what is set as default for that column, and automatically assign ID's, since it doesn't think you want to assign them yourself.
Also as a side note, if you import more than once without using TRUNCATE TABLE -- MySQL will pick up on the last inserted ID and move on from there, even if the table is empty. So if you ever want to start over from 1 you'll have to trucate the table.
I am receiving a list as input for a stored procedure in MySQL 5.6 and need to create a temporary table that has a a column (listOfUsers). Each item in the list, needs to be its own row for this column.
All the answers I've seen so far, show the list being used in a WHERE clause to filter a query. I am not trying to filter anything, just create a table with one column from a list.
Is this possible?
I am going to assume some specifics in the example, I hope my assumptions are relevant to your problem. Suppose we have a comma-delimited list on one line in a file. Then we can do:
create table t1 (s varchar(50));
load data local infile '/tmp/file.txt' into table t1
lines terminated by ',';
update t1 set s = replace(s, "\n", "");
The last update is needed to remove the spurious newline from the last column. Something based on this idea will hopefully solve your problem.
If the data is not coming from a file, a simple solution that requires minimum application coding is to put the data in a temporary file, and then apply the method above. Alternatively, you can just parse out the input in the application, and then build a multi-row insert.
I am extracting data from excel sheet into sql database using
Insert INTO dbo.tablex
Select * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0 Xml; HDR=YES; Database=C:\Users\xxx\Documents\xxx.xlsx',
'SELECT * FROM [xxx$]')
GO
Which works perfectly.
Now I need a new column in my table of date and it should automatically get the current date.
So I created date column with default as current_timestamp but now when I run the insert command it says columns does not match with schema of table definition.
I need date to be automatically filled in with current date in all rows once the insert command is fired.
If i understand correctly, its not possible from SQL and you would have to make changes in the Excel file by adding a column of Date.
I am attempting to import data into a table that has a field as follows:
result_id
This field is set to AUTO_INCREMENT, PRIMARY and UNIQUE.
The data I am importing has information in the result_id field that is the same (in places) as the current data in the table. SQL won't let me import as there are duplicates (which is fair enough).
Is there a way to get SQL to append the data I am importing and not use the duplicate data in result_id, basically to continue the number within the SQL field. The reason I am asking is that I am importing about 25,000 records and I don't want to manually have to remove or alter the result_id information from the data being imported.
Thanks,
H.
How are you importing your data to MySQL?
If you are using SQL queries/script, then there should be something like INSERT INTO.... Open the file in some text editor and replace all INSERT by INSERT IGNORE. This will ignore inserting rows with duplicate primary keys.
Or alternatively if you want to replace older data with same primary keys to that in your import script, then simply use REPLACE query in place of INSERT query.
Hope it helps...
[EDIT]
Since you have Primary key, auto increment. In your table in which you want to import data, add a dummy column say "dummy" and allow it to be NULL. Now, in your import script there will be statement like INSERT INTO () values (). Now in the list of column names replace "result_id" by "dummy" and execute the script.
After executing script simply remove "dummy" column from table. Though it is bit dirty and time consuming but will do your work.
I'm working on a database right now and I have a pretty specific problem that I'm trying to figure out:
I have a large master table in my database with all the info we're gathering. We're updating records in this master table based on Excel files returned to us by various team members across the company - all of the records have unique ID numbers so we know what fields in the master table to update. We are tracking who responds by updating the file name into the master table as well. I want to update this with the file name; however, if two sources give me the same data, I want to append the second file to the first file rather than replace it with an update.
The problem is, I need the query to "know" when to update and when to append. Is there some IF statement I can use - maybe Update when Null, Append when Not Null?
You can refer to an Excel sheet or range in a query:
INSERT INTO Table1 ( ADate )
SELECT SomeDate FROM [Excel 8.0;HDR=YES;DATABASE=Z:\Docs\Test.xls].[Sheet1$a1:a4]
WHERE SomeDate Is Not Null
This means that you can run queries based on the presence or absence of data in the Excel file.