SQL read csv formatting in tables - mysql

The data I have is formatted in this way:
Apfeltasche;E1412,E1442,E410,E500,E920,E471,E330 \
Apfeltüte;E300,E501\
Balsamico Dressing;E224,E150d,E262,E500,E415\
Barbecue Sauce;E415,E1404\
Big Mac;E471,E472e,E300,E481,E202,E263,E331,E330,E415,E322, E160a,E160c\
Butter;NULL\
Cheeseburger;E471,E472e,E330,E481,E300,E331,E202,E263,E322,E160a,E160c\
The first column is the name of a product and the codes on the right are additives (e-num). I have one table for products that combines the name with an auto-increment id. And I have a table that stores the connection between the product-id and the e-num.
So there are two problems: First I need to load the product-names into the product table to get a valid id for the product. Then I have to combine the ID with the e-nums. But the e-nums are seperated with commas which would have to be a seperate row each time in the table connecting id and e-num. So I would kind of have to do two splits.
Iam really not finding any way to do that. And unfortunatly Iam not allowed to change the csv data to fit my model better. How would you do that ? Thanks guys :)

Related

How do I combine two tables to just return one field and removing the duplicates

I have two tables with my inventory master data from two different locations but all sitting in one Access database. I need a query where I can create a new table showing all of the inventory codes for both inventory tables, but all duplicates needs to be eliminated. Is this possible and how would I go about it? I don't need all of the other information in the other tables, I am really only looking for the inventory codes.
select code from table A
union
select code from table B
Once you have the codes, you can use the codes to get the rest of the information from the necessary tables.

how to store multiple values in a single cell in mysql using command line

I have already searched a lot,but unable to give me solution.
I have only one database table having two columns : User Name and Email Id.
I don't want any mapping table.
I want to insert more than one email Id for a single user with a comma separator.
what should my query in MySQL ?? I am using command Line
1) when 1st time entry with unique user and multiple email Id and
2) when I found user is already exist and email Id field have multiple Ids but not the one I am searching for so concatenate it with existing values.
Can you please provide me queries for above two mentioned scenarios.
Thank you so much in advance...
quite easy
insert into tableName('userName','test1#gmail.com,test2#gmail.com,test3#gamil.com');

MySQL: How do I get a piece of information from one table based on it's id in another table

I have two tables: Suppliers and Unit_User.
The Suppliers table has a field called suppliersunit which contains a comma-separated list of ids. These are the ids of the Units in the Unit_User table.
I need the query to get the unit_name from Unit_User for each of the ids in the suppliersunit column.
I know this is relatively simple but I'm pretty new to MySQL so any help would be appreciated.
Comma separated values are bad design. If you can't redesign the table or if you are using someone else's table try using application code to parse and do the query. It would be unreliable to do it through SQL.

Removing the entries in a comma separated field based on the data of another table

First off I am using MySQL 5.0.96. (always forget to mention that)
I have two tables(TMP_VIN_STOCK and newvehicles) I load TMP_VIN_STOCK with our current inventory of vehicles daily.
newvehicles is our currently listed vehicles on our website table.
I want to find results in newvehicles that are NOT in TMP_VIN_STOCK. (So I can remove them from website because we sold them)
TMP_VIN_STOCK.name and newvehicles.stocknum are each tables stock number column.
TMP_VIN_STOCK.name has only single values of a stock number
newvehicles.stocknum can have multiple values separated by commas in it. (Same exact vehicle just multiple stock numbers)
Example:
TMP_VIN_STOCK table
- K12049
- S12040
- T12020
newvehicles table
- S12018, S12039, S12040
- Y13068, Y13093
- T12020
How can I find out what stock numbers are not in TMP_VIN_STOCK but are in newvehicles?
Your problem beautifully demonstrates why tables in relational databases should be normalized.
If you want to keep this design then you have to do something like this:
loop over the newvehicles rows:
loop over the items in the comma separated field
check if the item does not exist in TMP_VIN_STOCK
Not to mention that mysql does not have a function to split a comma separated list and
let alone the pain you should go through to remove the ones you find from the comma separated list.
You should definitely redesign your database. Have a table for vehicle data and another one for stock data and link them with foreign keys. That way you don't have to go through all this pain. You can retrieve the data you want with a simple query.
Also if an entry is not in the stock, its data should not be deleted but the count should be zero or it should be flagged as out of stock.
This question has some details that would let you split the values in newvehicles table and let you populate a (temp) table with the results then a not exists or not in query would give the results you need.
Alternatively you could keep the previous days load of TMP_VIN_STOCK (maybe add a date field into the table - then you could compare what you loaded today with what you loaded yesterday

Associating extra data with a MySQL column

I have a typical table, e.g.
id(int) name(varchar) address(varchar) date(datetime)
I also have a table that references validation functions for each one, e.g.
id(int) function(varchar) fail_message(varchar)
1 email Please enter a valid email address
2 required This field can not be left blank
I'd like to be able to associate each column from the first table with one or more of these validators.
The only way I can think of doing this is to stuff the ids into the column names e.g. (column name: email;1;2) and keep track of it through PHP, but that seems very messy.
Is there a good way to do this with relational databases? Would a NoSQL implementation suit this problem better?
Similar to what Dan said, a relatively easy way to implement an association in sql would be to do the following:
id(int) function_id(int) col_name(varchar)
1 1 address
2 1 second_address
3 2 address
4 2 name
And then when you want to do the failure check, use the above table to link the error message to the column name (e.g. 'select function_id from above_table where col_name="address"') and then query the failure table. These tables could subsequently be combined using a view with a join so that a single query would suffice.
Hope this helps.
put this in another table that describes the columns for tables oddly this is very much like extending the table that lists table columns with additional columns
let's say if you extend your example with say localized strings that would mean that the fail_message would become a fail_message_id and the table fail_message would have the columns (id, language, message)