Adding/Editing MYSQL keys - mysql

I am working on a table within a MySQL database that has the following columns:
- ItemID (auto incremented key)
- Item
- CategoryID (empty column)
- Category
- SubcategoryID (empty column)
- Subcategory
In other tables within the same database I have a list of categories and subcategories with their unique IDs, and I would like to take those IDs from the existing tables and port them over to the item table I described above. After porting the IDs over, I intend to delete the category and subcategory entries, as the ID will serve as enough reference for my purposes.
I could do this manually, and I will do it manually if necessary, but going through several thousand entries in MySQL doesn't sound like my ideal afternoon.
My question is simply, is there a way to use my existing tables to alter my item table (above) in the way I described?
Thanks!

So you have a category table with an id and category field and the category field matches items.category table.
To update the items.category_id, JOIN to the category table on items.category <=> category.category and UPDATE items.category_id with value in category.id
UPDATE items
JOIN category ON items.category <=> category.category
SET items.category_id = category.id;
You may have to change some columns names, but hopefully they are obvious.
Repeat this for subcategory and any other fields.
Note: <=> is NULL-safe equal, so rows with NULL values will get updated correctly

Related

Select rows where item is only in certain categories

So I have a somewhat complicated mysql query question. I have 3 tables. One is a table of items. One is a table of categories. And one is a linking table that just has 2 fields, itemID and categoryID. It is a many to many relationship, so one item can be in multiple categories and each category can have multiple items. Now two of the fields in the category table are isactive and ismain. They are just bools of 1 or 0. I want to grab all items that only belong to categories where at either isactive=0 or ismain=0 or both.
I took some time and set up a sql fiddle for someone to play around with. http://sqlfiddle.com/#!9/b03842/2
Solution using subquery:
SELECT DISTINCT i.* FROM cart_item i
JOIN cart_item_category ic ON i.itemref = ic.itemref
WHERE ic.catid IN (
SELECT id FROM cart_category WHERE active = 0 OR ismain = 0
)

MySQL join 2 tables on non-unique column and with timestamp conditions

I have 2 MySQL Tables: "parts_revisions" and "categories_revisions". My goal is to use the revisions data in these tables to create a log that lists out all the changes made to parts and categories. Listing the changes to "parts" in one single SQL statement has proven tricky though! Here is the situation:
All entries of each table have "timestamp" columns.
Every parts_revisions entry has a "categoryId" that basically links it to the categories_revisions table. (Every part is a child of a parent category.)
All I want to do is list out all the parts_revisions, but use the human-friendly "name" column from the categories_revisions table based on the categoryId column in parts_revisions. This will make the log more readable.
The trick is that, because there are usually multiple revisions for each category within the categories_revisions table, I cannot do just one big 'ol join on the categoryId column to get the name. The categoryId column is non-unique, and "name"s may vary. What I have to do is get the latest category_revisions entry that has a timestamp that is no later than the timestamp of the part_revisions entry. In other words, we want to get the appropriate category name that was in use AT THE TIME the part revision was made.
Not sure if this matches your table structure, but here's a go at it. It's a bit of an ugly subquery inside a subquery. Guessing it won't be terribly efficient
select part_name,
category,
(select name
from categories_revisions
where categories_revisions.match_id = parts_revisions.category
and categories_revisions.timestamp = (select MAX(categories_revisions.timestamp)
from categories_revisions
where categories_revisions.match_id = parts_revisions.category
and categories_revisions.timestamp < parts_revisions.timestamp)) as name
from parts_revisions;
http://sqlfiddle.com/#!2/da74e/1/0

create field merging info from other tables and fields

i need to create a field in my table in order to put a title to all my properties for sale. I am not a programmer but i studied a little mysql, and I cant find a solution to this.
I need to fill in a field in a mysql table using information from different tables, let me explain.
I have a table named "products" where the locality and category are defined with a number (id), then I have another 2 tables named "localities" and "categories" where a string is assigned to the id number of the "products" table, ex:
in products table i have a product with locality id n. 10 and category id n.17, in localities table the id n.10 has the value "locality 1" and in categories table the id n.17 has the value "houses for sale".
Well, i need to update the products table and set the "title" field using information from the other tables and create a title string for ALL ROWS using the info from the other tables, in order to have titles similar to the following one:
"Houses for sale in locality 10"
and for example
"Apartments for sale in locality 23"
"Land for sale in locality 34"
etc.
Please note that the word "in" is not part of any table so I need to insert it in the middle in some way. I hope i have been clear enough, if not please ask. Thanks.
You can do what you want using update with join. Here is an example:
update products p join
localities l
on p.localityid = l.locality id join
categories c
on p.categoryid = c.categoryid
set p.title = concat(c.value, ' in locality ', l.value);

Inner join with multiple matching records

I have 3 tables in my database: sProduct, sProductDetail and sProductDetailWarehouse. This is basically a webshop with having multiple EANs possible for a single product. For instance a t-shirt with multiple colors available, each color being it's own EAN.
The important bits about tables:
sProduct has ID which is primary key and title (varchar).
sProductDetail has ID (primary key), ID_sProduct (the correlation to the sProduct table), EAN and title
sProductDetailWarehouse has ID (primary key), ID_sProductDetail (correlation to the detail table) and stock (int).
What I would want is to use something similar to this:
select pd.ID,pd.title,pdw.stock from sProduct p
inner join sProductDetail pd on pd.ID_sProduct=p.ID
left join sProductDetailWarehouse pdw on pdw.ID_sProductDetail=pd.ID
and only have it return 1 record on join with the highest stock. The problem is I can't use order by since I have multiple products in a query needing to be ordered by their release date.
So basically out of every one sProduct.ID I would need only one sProductDetail.ID returned even though there might be many. Can anyone help with this?
Thanks.

Joins on MySQL many-to-many tables

This has been driving me mad.
I have three tables:
items
ID
name
type
cats
ID
name
items_to_cats
FK_ITEM_ID
FK_CAT_ID
This is a simple many-to-many relationship. I have items and categories. Each item can be linked to one or more categories. This is done via a simple joining table where each row maintains a relationship between one item and one category using foreign key constraints.
You will notice that my "items" table has a field called "type". This is an indexed column that defines the type of content stored there. Example values here are "report", "interview", "opinion", etc.
Here's the question. I want to retrieve a list of categories that have at least one item of type "report".
Ideally I want to get the result in a single query using joins. Help!
select distinct cats.id, cats.name
from cats
join items_to_cats on items_to_cats.fk_cat_id=cats.id
join items on items.id=items_to_cats.fk_item_id
where items.type='report'
Just as a point of database design, if you have a small set of legal values for items.type, i.e. "report", "interview", "opinion", maybe a couple more, then you really should create a separate table for that with, say, an id and a name, then just put the type id in the items table. That way you don't get into trouble because somewhere it's mis-spelled "raport", or even more likely, someone puts "reports" instead of "report".
or how about this :
SELECT c.id, c.name
FROM cats c
WHERE c.id IN
(SELECT ic.fk_cat_id
FROM items_to_cats ic
JOIN items i on i.id=ic.fk_item_id
WHERE items.type='report'
)