primary foreign key display in html table - php

ok here is it. i have two tables: products and warehouse
product table consist of (pid(primary),pname,pcolor) while my
warehouse table has (pid(primary foreign key=> products table),
date_delivered,quantity).
my question is can i display the warehouse table with pid,pname,pcolor,date_delivered and quantity since my primary key in warehouse table is the pid from products table? can i use join with these? if so how?
thank you.

SELECT *
FROM product
INNER JOIN warehouse
ON product.pid = warehouse.pid
Here an Inner Join is used.

select a.pid, pname, pcolor, date_delivered, quantity
from products a, warehouse b
where a.pid = b.pid;

Related

how to get the distinct data from a table and then join other table and take sum of 2 columns

I have two tables one table consisting of my order details and other table having the product details. I want to get the distinct products ordered and their id also sum of same products ordered and price
$query = $this->db->query('SELECT
distinct("productid,picture"),SUM(quantity) as `quantity`,SUM(price) as
`amount` FROM `order_details` LEFT OUTER JOIN `products` ON
`products.serial`=`order_details.productid` where `order_details.user_id` =
`$data[results]`');
return $query->result();
Above is the query i used and if an user ordered a product multiple times and multiple days means i want to get the distinct of product and its id from the table order_details as well as i need the sum of same orders as quantity and sum of price of the same product and join it with another table named products to get the product name based on productid stored on order_details table
Tables details:
My order_details table consists of (user_id,productid,quantity,price).
Under the productid 3,3,2 are the productids, and under the quantity 10,10,11 etc, and under the price: 64,64,396 etc.
The table products table have (serial,name,price,picture).
What i want is to show products with id 3 and 2 then take sum of quantity of each product and also take sum of price of each product. Actually this is for an ecomerce project. the user logged in to view his orders.
Try next query, first you have to inner join products with order_details, this way you can discard all products that was never ordered. Second, you group by the products common attributes, and then use aggregation methods over those groups.
$query = $this->db->query(
'SELECT
p.serial,
p.picture,
p.name,
SUM(o.quantity) AS total_ordered_quantity,
SUM(o.price) AS total_amount
FROM
products AS p
INNER JOIN
order_details AS o ON o.productid = p.serial
WHERE
o.user_id = $the_user_id
GROUP BY
p.serial, p.picture, p.name'
);
return $query->result();
You can check a working example of the query on next link:
http://sqlfiddle.com/#!9/c2a1b0/1

CI Active record, Select record from multiple table

I have two table. First is product in which Admin will add the Products and another is product_detail in which vendors will add the detail. Multiple vendors can add same product with the product detail like selling price, offer price etc.
"I want to select products from products table and want to select product detail from product_detail table but only one row of product detail which has less selling price".
So i want to use CI active records to get the final output as product list with its detail.
Take this for example :
Create table #Product(id int, name varchar(100));
Create table #Product_detail(id int, product_id int, detail varchar(100));
insert into #Product values
(1,'Item1'),
(2,'Item2')
insert into #Product_detail values
(1,1,20),
(2,1,10),
(3,1,40),
(4,2,70),
(5,2,50)
this is your basic query:
select
a.*, b.price
from
#product a inner join
(select product_id, min(price) price
from #product_detail
group by product_id
) b
on a.id = b.product_id
In CI, try to do something like this:
$var = $this->db->query("select a.*, b.price from #product a
inner join
(select product_id, min(price) price
from #product_detail
group by product_id
) b
on a.id = b.product_id")->result_array();
sample output :
id name price
----------- ------ -----------
1 Item1 10
2 Item2 50
You first should make getters and setters in your model and using CI active record + pdo It'as more safe from my point of view.
Best regards.

Join three rows with one row from another table

I am working on a home project. In my database I have two table called movies and category. in the movies table there are three rows named category,category_two,category_three. And in the category table there are only one row name category_name.
I wanted to join all three rows from the movies table with the row named category_table from category table.
but I do not know how to do that query.
Please help me.
This is how you should do it:
movies table
------------
id
name
categories table
----------------
id
name
movie_categories table
----------------------
movie_id
category_id
Like this you can have any amount of categories for a movie. Then to get a specific movie along with its categories you can do:
select m.*, c.name as category_name
from movies m
left join movie_categories mc on m.id = mc.movie_id
left join categories c on c.id = mc.category_id
where m.name = 'star wars'

Using Indexes in mysql correctly

I would like to know that how can I make a proper Indexed table, to understand this concept I will be using movies as example:
I have these 5 tables with multiple fields but I will list here these tables primary indexes fields only:
movies
movie_id = primary index
actors
actor_id = primary index
geners
gener_id = primary index
reviews
review_id = primary index
And then I have these tables for relation with 2 columns each, I am unsure what type of indexes should have these relational tables:
movie_actor
movie_id,actor_id
movie_gener
movie_id,gener_id
movie_review
movie_id,review_id
I have join on these fields If I wants to get one movie details I will use such query:
SELECT *
FROM movies as m
LEFT JOIN movie_actor AS ma ON ma.movie_id = m.movie_id
LEFT JOIN actors AS a ON a.ator_id = ma.actor_id
LEFT JOIN movie_gener AS mg ON mg.movie_id = m.movie_id
LEFT JOIN geners AS g ON g.gener_id = mg.gener_id
LEFT JOIN movie_review AS mr ON mr.movie_id = m.movie_id
LEFT JOIN reviews AS r ON r.review_id = mr.review_id
WHERE m.movie_id = 1234
So what kind of index should I use on relational tables (movie_actor,movie_gener,movie_review) both fields, primary on which one or just index on both?
Thanks
Only one field can be the primary key on each table Your table can only have 1 primary key (corrected by user). Look at this post for information Differences between INDEX, PRIMARY, UNIQUE, FULLTEXT in MySQL?

What should be PHP/MySQL query for the following requirement?

It is a shopping cart, where user can add product to watchlist. Have a seprate page of User's watchlist where all product apepears that has been added to watchlist by that particular user. product_images table is separated from product table since 1 image can have multiple images.
MySQL Tables
Table "product" :
pid(pk),name,price, etc...
Table "watchlist" :
wid(pk),pid(fk),userid(fk)
Table "p_images" :
image_id(pk),pid(fk),image_name
I want to fetch all information from product table and only 1st image of that particular product from p_images table.
product should be fetch by pid in watchlist table.
select * from product p
inner join p_images i on p.pid = i.pid
where p.pid = 1
group by p.pid
order by image_id
I found it accurate using LEFT JOIN check following query.
SELECT *
FROM product p
LEFT JOIN P_images i ON p.pid = i.pid
GROUP BY i.pid

Categories