I am trying to return data for someone in the following format from a SQL 2008 query -
ProductID, ProductTitle
ServiceID, ServiceTitle
ServiceID, ServiceTitle
ProductID2, ProductTitle2
ServiceID, ServiceTitle
ProductID3, ProductTitle3...
So the product table lists the products and then the product service table will have several services assigned to one product ID. Is the above even possible? Or something similar?
It is very possible, and it's a good practice when you're just beginning SQL,
this is how your database/table structure should be:
t_Products : ID, Name, Price
t_ProductService:
ID
serviceName
productID
the ID in t_Products should be primary key and
the productID in t_ProductService should be the foreign key of the ID in t_Products
Edited:
You need to use join, there are a lot of join in SQL,
SELECT * FROM t_ProductService ps
INNER JOIN t_Products p
ON ps.ProductsID = p.ID
Related
I have a good idea of the pseudo-logic of what I want to do - just struggling to think of the syntax to put it into practice.
I’ve got three tables:
product_images
product
product_import
At a high level - I want to insert a row into the product_images table with just two values (image_url, product_id) - the image url can be found in the product_import table along with an product_id. This product_id is the old ID of the product (migrating from another system) - this is recorded as old_id in the product table.
Therefore the retrieval of the image_url works conditionally on the basis that: the product_id in the product_import table has a match with the old_id value in the product table. If it does match - then insert the value of the matching image_url from the product_import table and the new product_id that matches from the product table (if the old_id is found)
My guess at the SQL statement is something along the lines of:
INSERT INTO product_image(image_url, product_id)
SELECT product_import.image_url, product.id WHERE product.old_id = product_import.id;
When you want to write an INSERT INTO ... SELECT ... statement, the first rule is to start by designing the SELECT ... part.
In your case, the select query is syntactically invalid because it is missing a FROM statement.
So a valid select would be something like:
SELECT product_import.image_url, product.id
FROM product_import
INNER JOIN product ON product.old_id = product_import.id;
You may have to tweak it a bit based on your needs but it is the minimum query to start working. And of course you add the INSERT part only when you are satisfied with your SELECT query.
This was what I needed in the end..
INSERT INTO product_images(image_url, product_id, account_id, is_thumbnail, created_at, modified_at)
SELECT `value`, id, account_id, "1", now(), now()
FROM product_import
JOIN product ON product_import.entity_id = product.old_id
WHERE product.account_id=1;
Today I passed an interview and thez ask me this :
Write pseudo SQl statements to create database tables to store the products of a basic websho. Each product has a name, a price, a creation date ans may belong to several categories. Categories have a name and a flag to indicate whether the category is private or public.
Also:
Write a SQL query to find the list of products that belong to more than 5 public categories.
My knowledge of sql is pretty limited, i just pass 2 hours searching on the web for pseudo-sql statements without result.
Can someone explain me what it is and eventually respond to the answers so i'll know ? (interview already fail so you dont do my homework ahahah)
Thanks!
product(pro_id, pro_name, price, date)
pro_id is the Primary key
category(cat_id, cat_name, cat_flag, cat_type)
cat_id s the primary key
procat(pro_id, cat_id)
pro_id, cat_id are primary key together
Query:
select pro_id from procat where cat_id in (select cat_id from category where cat_type = 'public') group by pro_id having count(*)>2
there is a website which used to sale physical products ... now they want to sale some king of service which is completely different .... like credit packages to charge user account(to buy products) or buy sms .. stuff like that
so db used to look like this
order : user_id ,date , total_price
order_items : order_id , item_id , quantity , price
invoice : order_id , price , settled
Invoice_transactions : invoice_id , amount , date
basically invoice belongs to orders
now they want to sale services , so we have to create 2 orders table
product_orders (it used to be orders)
service_orders
now i have to choose a design for invoice table
i can add add another field to indicate type pf the order
invoice : order_id , order_type , amount
1 , product , 10000
1 , service , 10000
which somehow doesn't feel right ... or i can add invoice id to orders
invoice : order_type , amount , date
product_orders : invoice_id , date , total_price
service_orders : invoice_id , service_id , date , total_price
which one(if any) seems to be more reasonable design ?
here is products table :
here is what service tables would look like :
here is orders (which should change to product order )
here is what service_order would look like
I presume that both product orders and service orders have more in common than they have in difference. Within the greater business and financial ecosystem, they likely share the same roles/functions. So my first instinct to have a single table for both kinds of orders.
One-To-One tables
Another route is to have simply one order_ table, with a column indicating which type of order named something like order_type_ containing values like sales or products.
Move all the product-related columns to a separate child table, product_info_. Move all the sales-related columns to a separate child table, service_info_. I would make order_ table the parent to both, though technically we have a One-To-One relationship. For any given order_ row we have exactly one product_info_ row or exactly one service_info_ row.
This might minimize duplication of data in the database and coding in your app. Your app code uses the flag field order_type_ to query one or the other child table appropriately when you need the product or service details.
The invoices table links against the orders table. I assume invoicing works pretty much the same for both types of orders.
All fields in Order
Another alternative is simply including all the product and service fields within the order_ table. Simply ignore the product-related fields for the service orders, and ignore the service-related fields for the product orders. Not the most elegant, but if you have relatively few fields, and perhaps prefix the column names with prod_ and serv_, this may be practicable. I have certainly done this more than once.
It all depends on what you want to achieve with it, can a customer buy both in one order? The most common is what you did with invoice with an order_type it will have the least duplication
This is what I would try to do:
Table types
type_id | name
1 | product
2 | service
Table items would have common columns for both - products and services.
items: item_id, type_id FK(types.type_id), price, title, ...
Tables products and services with FK item_id and columns that are not common for both types (like products.weight).
Add type_id column to the orders table:
order : user_id, date, total_price, type_id FK(types.type_id)
Add type_id column to the table order_items:
order_items: order_id, item_id, type_id, quantity, price
The FKs would be:
(order_id, type_id) -> orders(order_id, type_id)
(item_id, type_id) -> items(item_id, type_id)
With those FKs it's not possible to mix different types in one order.
I have the tables and i am trying to get the active name(s) of a product by using the tracking Id.
These are the tables i have:
tblactive: tblProductcode: tblproduct tblCV
client_name client_name client_name Tracking_id
Product_name product_name product_name product_code
active_name product_code status
color
I want to be able to search for the active name when i key in the tracking id:
I have been thinking about this for awhile but just cannot figure out... probably monday blues.
This is the sql query i have currently have:
SELECT tblActive.active_name, tblActive.l4
FROM (tblActive INNER JOIN tblProductCode ON
(tblActive.Client_Name = tblProductCode.Client_Name)
AND (tblActive.Product_Name = tblProductCode.Product_Name))
INNER JOIN tblCV ON tblProductCode.Product_Code = tblCV.Product_Code
WHERE (((tblcv.product_code)=[forms]![frmCVSwabRequest]![cboProduct_code]));
This query just returns a blank.
Why dont you just make a unique primary key named tblactive_id for table tblactive
and add it to the tblCV table. Then simply run this query,
SELECT tblactive.active_name FROM tblactive, tblCV WHERE
tblactive.tblactive_id = tblCV.tblactive;
I think you need to redesign your database tables properly.
A mysql one to many question: various products and where I sell them. I have a variety of products that I can sell, and several locations to sell them. I have a products table with a Primary Key: product_id. I have a locations table with PK location_id and a field product_id to link to the products table.
So, SELECT * FROM products LEFT JOIN locations on product_id.
I got an ambiguous column error. That doesn't make sense, they should line up.
So: SELECT * FROM products LEFT JOIN locations on products.product_id = locations.product_id.
That worked.
Here's the problem: there are many products I can get but don't currently sell. Some products have no locations. To create a link to add a location, I need the product_id. If I print out $row['product_id'] I am getting the product ids from the locations table, which is blank if they are unsold. I need the product ids from the first table.
This can't be an unusual situation.
Ideas?
You need to specify in your SELECT statement that you want to use the product_id from the products table. Here is a sample of what I am talking about (SQL Fiddle):
SELECT p.product_id, p.description, p.unit_of_measure, l.location
FROM products p
LEFT JOIN locations l on l.product_id = p.product_id
As you can see for product_id 3 there is no location but yet I am still displaying the product_id.
MySQL supports the using clause, so you can write the first version as:
SELECT *
FROM products LEFT JOIN
locations
USING (product_id);
Without the using clause, MySQL needs to know the table where a column comes from. Because there are two tables with the same column name, MySQL is confused.
This also has the advantage that the * only includes the product_id column once, not twice.