i have a bit of a problem, i have two MySQL tables namely products and categories. The categories table has an id as the primary key and name of the category. The category id is a foreign key in the products table. I want to create a view, but i don't want the view to contain the id as the category,instead i want the category name to be shown in the view. I have tried every kind of JOIN but I'm still not getting the required result. What am i doing wrong? here is my latest join. Which is pretty much the closest to what i want.
SELECT c.title as title,ct.name as category, c.picture as picture
FROM companies c left join categories ct on ct.id=c.category
I have successfully gotten the title and picture, but the category remains null.
Thanks for the replies.. Michael turns out that you were right , I mixed up the tables when performing the insert via my web interface so there wasn't a match. Thanks a lot!
Related
I have a MySQL statement that I want to use that will display the data in two different tables, but not have any duplicated data.
SELECT Customer.firstName, Customer.lastName, Purchase.productName, Purchase.productPrice
FROM Purchase
INNER JOIN Customer
This is currently the MySQL I am using and it does work, but it loads duplicated data which I do not want. I have looked around but not seeing a simple solution. Sorry in advance if it is a simple solution, been working for awhile and brain isn't really working.
You have to bind those tables via related columns.
Let's assume primary column of table Customer is named ID and customer ID is being held in a column named customerID in table Purchase:
SELECT Customer.firstName, Customer.lastName, Purchase.productName, Purchase.productPrice
FROM Purchase
INNER JOIN Customer
ON Custormer.ID=Purchase.customerID
I have a website with products where some products are duplicated, and the duplication is only because sometimes the same products goes under more than one categories. I just want the unique columns of the product, not the duplicate (that has another ID and another Category_id). I know the problem could be solved if the table was normalized, but I didn't develop these tables and I can't redesign the database now.
So basically I'm trying to something that logically looks like this (but the code below still gets the repeated products):
SELECT id
FROM `website_products`
WHERE p_name_en
IN (
SELECT DISTINCT p_name_en
FROM `website_products`
)
Do you just want:
select distinct id
from website_products
Or are you trying to get distinct product names with a single id:
select p_name_en, id
from website_products wp
group by p_name_en;
You Can Try like this,,,
SELECT id
FROM `website_products
group by p_name_en
I am developing web application where I have to implement 'Likes' system as facebook has. Application will have a few categories of products that customer can 'like'. So I have started to create database, but I stuck on one obstacle. As I understand there are two ways of doing this:
First. Create one database table with fields of "id, user_id, item_category, item_id". When user click 'like' button information will be saved in this table with various categories of products (item_category).
Second. Create several tables for certain categories of item. For instance, "tbl_item_category_1, tbl_item_category_2, tbl_item_category_3" with fields of "user_id, item_id".
Would be great to get more insight about best practices of this kind database structures. Which works faster? and more logical/practical? I will use only several categories of items.
I would go with the first version with a table structure similar to this:
User Table: PK id
id
username
Category Table: PK id
id
categoryname
Like Table: PK both user_id and catgory_id
user_id
category_id
Here is a SQL Fiddle with demo of table structure with two sample queries to give the Total Likes by user and Total Likes by category
The second one - creating multiple tables is a terrible idea. If you have 50-100 categories trying to query those tables would be horrible. It would become completely unmanageable.
If you have multiple tables trying to get a the total likes would be:
Select count(*)
from category_1
JOIN category_2
ON userid = userid
join category_3
ON userid = userid
join .....
Use one table, no question.
The first method is the correct one. Never make multiple tables for item categories, it makes maintaining your code a nightmare, and makes queries ugly.
In fact, the general rule is that anything that is dynamic (i.e. it changes) should not be stored as a set of static objects (e.g. tables). If you think you might add a new type of 'something' later on, then you need a 'something' types table.
For example, imagine trying to get a count of how many items a user has liked. With the first method, you can just do SELECT COUNT(*) FROM likes WHERE user_id = 123, but in the second method you'd need to do a JOIN or UNION, which is bad for performance and bad for maintainability.
The first method is the correct one. Because you dont know how many categories you will be having and it is very difficult to get the data.
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'
)
I’m currently in the process of developing my own blogging system. Currently when you create a new post, you get the option to archive it categories of your own choise.
Currently I’m storing the categories as a VARCHAR value in a mysql database. As an example the field will contain 2,4,8 if the user has chosen the categories with ID: 2, 4 and 8.
To retrieve the blog posts for the category with ID 4 I then use:
SELECT col FROM table WHERE LOCATE(',4,', CONCAT(',',col,','))
I’ve been told that values seperated with a decimal comma is a no-go (very bad) when it comes to good database structure!
Can anyone provide me with a good way/technique to make this the most effective way?
Thanks in advance
A flexible & robust setup, as posted so many times in SO:
POSTS
id
name
text
CATEGORIES
id
name
POST_CATEGORIES
post_id
category_id
Where the current query would be:
SELECT p.id, p.name, p.text
FROM posts p
JOIN post_categories pc
ON pc.post_id = p.id
AND pc.category_id = 4;
Look into relational database normalization. For your specific case consider creating 2 additional tables, Categories and BlogCategories in addition to your Blog content table. Categories contain the definition of all tags/categories and nothing else. The BlogCategories table is a many-to-many cross reference table that probably in your case just contains the foreign key reference to the Blog table and the foreign key reference to the Categories table. This allows 1 Blog entry to be associated with multiple categories and 1 Category to be associated with multiple Blog entries.
Getting the data out won't be any more difficult than a 3 table join at worst and you'll be out of the substring business to figure our your business logic.