MySQL Sum with Join - php

I have been trying to do a sum with a join with no luck. What I need is to get a total from product_price (located in tbl_products) based on a product_id which is located in both tbl_basket and tbl_products.
So far I have:
$result = mysql_query("SELECT * FROM tbl_basket a INNER JOIN tbl_products b ON a.product_id = b.product_id WHERE a.customer_id = '" . $_SESSION['user'] . "'");
This works to get the product_name from tbl_products based on the product_id stored in tbl_basket. I know I need a COUNT which I have tried numerous ways but with no luck. I have chosen to show a query I know works for something as my syntax for the COUNT I tried was way off. Thank you.

Does this not work?
$result = mysql_query("SELECT sum(b.product_price) as total_price FROM tbl_basket a INNER JOIN tbl_products b ON a.product_id = b.product_id WHERE a.customer_id = '" . $_SESSION['user'] . "'")

Related

Fetching data from two tables using a specific criteria

What I am doing is: Fetching data to show the products to user, so that he/she can rate products. Which is working fine using below query.
$query = " SELECT table_products.product_name,
table_products.product_expiry,
table_users.name,
table_users.email
FROM table_products
INNER JOIN table_users ON table_users.user_id = table_products.user_id
ORDER BY table_users.id DESC";
Now what I want to ask is: How to show only those products that the user hasn't rated yet. Once a user rates a product, that product should not be fetched by the query for that user again.
I've 3 tables
table_users (user_id, name, email)
table_products (product id_, user_id, product_name, product_expiry)
table_rating (rating_id, user_id, product_id, rating)
and I want to fetch data from 2 tables
table_users, table_products
data to be fetched
table_users.name,
table_users.email,
table_products.product_name,
table_products.product_expiry
How to set query for this task for a specific user_id?
Thanks for your time and help :)
There are multiple ways, You need join table, group by and use having clause, please refer to the image.
Use this query
SELECT table_products.product_name,
table_products.product_expiry,
table_users.name,
table_users.email
FROM table_products
INNER JOIN table_users ON table_users.user_id = table_products.user_id
LEFT JOIN table_rating on table_products.product_id=table_rating.product_id
WHERE table_rating.product_id IS NULL
ORDER BY table_users.id DESC
You can join the ratings table and count the number of ratings then check it in the where clause, something like this (untested):
$query = "SELECT
table_products.product_name,
table_products.product_expiry,
table_users.name,
table_users.email,
count(table_rating.id) as ratings
FROM table_products
INNER JOIN table_users ON table_users.user_id = table_products.user_id
left join table_rating ON table_rating.product_id = table_products.id
where ratings = 0
ORDER BY table_users.id DESC";
SELECT
table_products.product_name,
table_products.product_expiry,
table_users.name,
table_users.email
FROM table_products
INNER JOIN table_users ON table_users.user_id = table_products.user_id
LEFT JOIN table_rating ON table_products.user_id = table_rating.user_id
AND table_products.product_id = table_rating.product_id
WHERE table_rating.product_id IS NULL
ORDER BY table_users.user_id DESC

Error on getting information in mysql

I have a public function with query but I don't get all the informations correctly.
public function getNextId($product_id,$product_category_id) {
$query2zz = $this->db->query("
SELECT *
FROM oc_product_to_category a
LEFT
JOIN oc_product b
ON a.product_id = b.product_id
LEFT
JOIN oc_product_description c
ON c.product_id = a.product_id
LEFT
JOIN oc_url_alias d
ON d.query LIKE CONCAT('%', 'a.product_id', '%')
WHERE a.product_id > '" . (int)$product_id . "'
AND a.category_id = '" . (int)$product_category_id . "'
AND b.status = '1'
ORDER
BY a.product_id ASC
LIMIT 1");
The idea is that I want to get next id for a next button. If you visit a product, you can click on next button, and you will go to next product on same category (clothes).
The problem is on oc_url_alias where I should get url for next id, as i mentioned here WHERE a.product_id > '" . (int)$product_id . "' but sometimes it get me wrong url (I mean get url for another product).
Thanks!

SQL Query JOIN syntax

I have the following tables and would like to query one more element from them.
categories table ->idcat(int), cat(varchar);
topics table ->idtopic(int), topic(varchar), idcat(int-fk), iduser(int-fk);
replies table ->idreply(int), reply(varchar) iduser(int-fk), idtopic(int-fk)
users table ->iduser(int), username(varchar).
My current query is;
$query = "SELECT t.topic, t.idtopic, u.username
FROM topics t
LEFT JOIN categories c ON t.idcat = c.idcat
LEFT JOIN users u ON t.iduser = u.iduser
WHERE c.idcat = '" . $idcat . "'";
Which presents 'Topic' and 'Username'. I'd like to show 'idReply' as well but don't know the proper JOIN syntax.
SELECT
t.topic,
t.idtopic,
u.username
FROM
topics t
LEFT JOIN
categories c ON t.idcat = c.idcat
LEFT JOIN
users u ON t.iduser = u.iduser
LEFT JOIN // new
replies r ON r.iduser = u.iduser AND r.idtopic = t.idtopic // new
WHERE c.idcat = '" . $idcat . "'";
This will generate a row for every reply to every topic in the specified category. Which could be a lot of records.
You may also want to experiment with the exact type of join on the replies table to get the result you want. LEFT JOIN is probably correct as you'll still get a result if there's no reply to a given topic. This may depend on your flavour of SQL.
OUTER JOIN & LEFT OUTER JOIN are possibilities.
Using INNER JOIN will ensure only topics with replies are returned.
You must have reply_id (foreign key) in your topics table. After that you can use this query.
$query = "SELECT t.topic, t.idtopic, u.username, r.id as reply_id
FROM topics t
LEFT JOIN categories c ON t.idcat = c.idcat
LEFT JOIN users u ON t.iduser = u.iduser
LEFT JOIN replies r ON t.reply_id = r.id
WHERE c.idcat = '" . $idcat . "'";

pull data from multiple tables in one query and matching data together

OK I have two sql tables I need to query from which are product and product_to_category
product looks like
product_to_category looks like
I am trying to query both the tables so that I can select all that are in a certain category number, which is in product_to_category, but also select all that have a status of 1, which determines if they are active or not. I am trying to do this in a single query, and so far my query looks like what is below, problem is that I am not sure how to match the product_id's together to work how I would like. Can someone give me an idea of how to do this?
mysql_query("SELECT * FROM product,product_to_category WHERE product.status = 1 AND product_to_category.category_id = 35 ORDER BY product.sort_order ASC")
Try:
SELECT * FROM product
INNER JOIN product_to_category ON (product.product_id = product_to_category.product_id)
WHERE
product_to_category.category_id = 35
AND
product.status = 1
you should use a JOIN
SELECT *
FROM product p
INNER JOIN product_to_category pc
ON p.productid = pc.productid
WHERE p.status = 1
AND pc.category_id = 35
ORDER BY p.sort_order ASC
since you are not familiar with joins, I suggest reading the following:
A Visual Explanation of SQL Joins:
http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html
Try this:
mysql_query("SELECT * FROM product JOIN product_to_category ON (product.product_id = product_to_category.product_id) WHERE product.status = 1 AND product_to_category.category_id = 35 ORDER BY product.sort_order ASC")
Try following query. It is easier to comprehend
SELECT * FROM
product a
JOIN product_to_category b ON a.product_id = b.product_id
WHERE
a.stock_status_id = 1
AND b.category_id = 35;
mysql_query("SELECT * FROM product p LEFT JOIN product_to_category pc ON (p.id = pc.product_id) WHERE p.status = 1 AND pc.category_id = 35 ORDER BY p.sort_order ASC");
This should do the trick. Make sure you look in to JOINS and their different types

Optimizing Small MySQL Join Query

I have a list of venues in the Venues table, and a list of a cities/states in the Locations table. The venue is associated with an area code unique to my organization, referred to as a SOYID. The SOYID is made up of a geographical area - each row in the Locations table has a City, State, and the corresponding SOYID. Some Venues rows have a SOYID, others do not; for those that do not, I need to find the SOYID for the city and state listed. I only want to select those Venues in a specific SOYID.
This query works, however, it takes a few seconds to load; I don't think I am writing the query correctly. Currently Venues has approx 140 rows, Locations has 40,000.
$sql = "SELECT DISTINCT a.VenueID, a.Name, a.PhotoID, a.City, a.StateAbbr
FROM Venues AS a LEFT JOIN Locations AS c ON a.City = c.city
WHERE a.SOYID = '" . mysql_real_escape_string($SOYID) . "'
OR ((c.city = a.City) AND (c.state = a.StateAbbr) AND (c.SOYID = '" . mysql_real_escape_string($SOYID) . "'))
ORDER BY a.Name ASC";
Any time you reference a column from a LEFT JOINed table (c.state and c.SOYID in your specific case) in the WHERE clause, you force that join to behave like an INNER JOIN. Instead, make those tests part of the join condition:
"SELECT DISTINCT a.VenueID, a.Name, a.PhotoID, a.City, a.StateAbbr
FROM Venues AS a
LEFT JOIN Locations AS c
ON a.City = c.city
AND a.StateAbbr = c.state
AND c.SOYID = '" . mysql_real_escape_string($SOYID) . "'
WHERE a.SOYID = '" . mysql_real_escape_string($SOYID) . "'
OR c.SOYID IS NOT NULL /* LEFT JOIN found a matching row */
ORDER BY a.Name ASC"
EDIT: Based on comments, this version should allow you do remove the DISTINCT requirement:
"SELECT a.VenueID, a.Name, a.PhotoID, a.City, a.StateAbbr
FROM Venues AS a
WHERE a.SOYID = '" . mysql_real_escape_string($SOYID) . "'
OR EXISTS(SELECT NULL
FROM Locations AS c
WHERE a.City = c.city
AND a.StateAbbr = c.state
AND c.SOYID = '" . mysql_real_escape_string($SOYID) . "')
ORDER BY a.Name ASC"

Categories