create field merging info from other tables and fields - mysql

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);

Related

How to select comma-separated values from a field in one table joined to another table with a specific where condition?

I'm working on a mysql database select and cannot find a solution for this tricky problem.
There's one table "words" with id and names of objects (in this case possible objects in a picture).
words
ID object
house
tree
car
…
In the other table "pictures" all the information to a picture is saved. Besides to information to resolution, etc. there are especially informations on the objects in the picture. They are saved in the column objects by the ids from the table words like 1,5,122,345, etc.
Also the table pictures has a column "location", where the id of the place is written, where I took the picture.
pictures
location objectsinpicture ...
1 - 1,2,3,4
2 - 1,5,122,34
1 - 50,122,345
1 - 91,35,122,345
2 - 1,14,32
1 - 1,5,122,345
To tag new pictures of a particular place I want to become suggestions of already saved information. So I can create buttons in php to update the database instead of using a dropdown with multiple select.
What I have tried so far is the following:
SELECT words.id, words.object
FROM words, pictures
WHERE location = 2 AND FIND_IN_SET(words.id, pictures.objectsinpicture)
GROUP BY words.id
ORDER BY words.id
This nearly shows the expected values. But some information is missing. It doesn't show all the possible objects and I cannot find any reason for this.
What I want is for example all ids fo location 2 joined to the table words and to group double entries of objectsinpicture:
1,5,122,34
1,14,32
1,5,14,32,34,122
house
...
...
...
...
...
Maybe I need to use group_concat with comma separator. But this doesn't work, either. The problem seems to be where condition with the location.
I hope that anyone has an idea of solving this request.
Thanks in advance for any support!!!
This is a classic problem of denormalization causing problems.
What you need to do is store each object/picture association separately, in another table:
create table objectsinpicture (
picture_id int,
object_id int,
primary key (picture_id, object_id)
);
Instead of storing a comma-separated list, you would store one association per row in this table. It will grow to a large number of rows of course, but each row is just a pair of id's so the total size won't be too great.
Then you can query:
SELECT w.id, w.object
FROM pictures AS p
JOIN objectsinpicture AS o ON o.picture_id = p.id
JOIN words AS w ON o.object_id = w.id
WHERE p.location = 2;

Can a table have multiple records of a single column in a single row?

There is a table 'items' with columns : item_name, i_code, items_left & price. It stores all the items that a shop sells. There is another table 'customers'. It stores record of all the customers who visited the shop. I want to keep record of all the items that a particular customer bought. I want to create a column 'items_bought' in 'customers' table, that will store item codes of all the items a particular customer bought. But having more than one item code for a particular customer row is impossible. Please help me have multiple records of items_bought in a single row of customers.
this is possible,but not suggested,
you can save your item code in customer table with or with comma or environment.newline
but it has not any use except display
my suggested solution is create a new table CustomerItem, having CustomerId,ItemId and other common attribute, that should b between Customer& Item like purchase rate, time of purchase etc(you cannot do it in above method)
The answer is, yes you can have multiple values in a single field for each row. A comma separated field could be a solution.
If you are using a relational database, your life will be easier if you create a new table, let's say items_bought, that will hold the relation between customer and item bought.
For example
create table `items_bought`
(id int not null primary key,
item_id int not null,
customer_id int not null)
Each field item_id and customer_id will have a foreign key to items.id and customers.id table fields respectively.
This way you don't need to manage strings and parse comma separated values. You can simply query your tables like:
select *
from `items` i
inner join `items_bought` ib on i.id = ib.item_id
inner join `customers` c on ib.customer_id = c.id
The above query will return all customer and item information of customers that have bought at least one item.

SQL returning all rows in one table where a column valule is equal to matching column value joined to another table

For an example lets say I have table INFO that contains columns:
ID - Name - Address
I also have a second table PURCHASES that contains columns:
Region - Name - Purchases
Multiple people can be in the same region, but each person only has one ID.
I want to write a query that will, based on a given ID in the INFO table, return all the rows in PURCHASES of people who live in the same region as the person with the specified ID.
I have done an Inner Join on Name for the two tables but can't figure out the best way to write a query.
Edit: My main problem is that there is no Region column in INFO. The only way to get the region is by joining to the PURCHASES table. Then I need the results of all rows containing that Region.
I am not sure if this is exactly what you want but you could probably twik it a bit to better fit your needs:
SELECT
Purchasse
FROM
PURCHASSE
INNER JOIN
INFO ON INFO.Name = PURCHASSE.Name
WHERE
INFO.ID = yourID
This should give you Purchasse for any given ID that the Name match for the two columns.
Try this:
Select * from PURCHASES LEFT OUTER JOIN INFO ON PURCHASES.NAME = INFO.NAME WHERE INFO.ID = givenID

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'
)