Fetching data from two tables using a specific criteria - php

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

Related

PHP/MYSQL counting orders

Here is database table:
$sql[2] = "SELECT u.* , oi.* , COUNT(oi.user_id) AS count
FROM users u, order_items oi
WHERE u.id=oi.user_id ";
$result3= mysqli_query($conn,$sql[2]) or die(mysqli_error());
if (mysqli_num_rows($result3) > 0) {
while ($record = mysqli_fetch_array($result3)) {
echo $record['count'];
}
}
I want to count how much order have every user. Example: Like Thomas have 3 order, but my code is writing 4, i want to write Thomas (3), Gracian(1). Any idea how to fix it ?
Use this query:
SELECT u.id,
COUNT(oi.user_id) AS orderCount
FROM users u
LEFT JOIN order_items oi
ON u.id = oi.user_id
GROUP BY u.id
The reason we count user_id from the order_items table is because of the edge case where a given user has no orders. In this case, we want to make sure that his count would appear as zero. The COUNT function ignores NULLs, which is what we want.
There is another way to perform the sql query using subqueries:
SELECT id,
email,
address,
name,
(SELECT count(user_id) FROM order_items WHERE user_id = users.id) AS orderCount FROM users;

Difficulty with join 3 tables in a query in php

My database has 3 tables i wish to access in the select query but I cannot seem to get it to work. Selecting from 2 tables works fine so I know everything else is working apart from my code for selecting from 3 tables. My database has been created on PHPmyadmin
The Tables are as follows:
forum_replies
reply_id
topic_id
user_id
reply_text
reply date
forum_topics
topic_id
category_id
user_id
topic_title
topic_description
topic_date
users
user_id
username
This is the code I have tried to use and shows the fields I wish to select:
$queryreply = "SELECT forum_replies.reply_id, forum_replies.topic_id, forum_replies.user_id,
forum_replies.reply_text, forum_replies.reply_date, users.user_id, users.username
forum_topics.topic_id,forum_topics.topic_title, forum_topics.topic_date
FROM forum_replies
JOIN forum_topics
ON forum_replies.topic_id = forum_topics.topic_id
JOIN users
ON forum_replies.user_id = users.user_id
";
$result = mysql_query($queryreply) or die (mysql_error());
$row = mysql_fetch_array($result);
Example in code would be appreciated. Thanks
Use this query:
SELECT a.reply_text, a.reply_date, b.topic_title, c.username
FROM forum_replies a
LEFT JOIN forum_topics b ON a.topic_id=b.topic_id
LEFT JOIN users c ON a.user_id=c.user_id
// apply WHERE, ORDER, GROUP if needed
Apart from syntax errors, you should use LEFT JOIN and table alias in your case.
To show also the topic creator's username, you can adjust the query to the following:
SELECT a.reply_text, a.reply_date, b.topic_title, c.username AS reply_user, (SELECT username FROM users WHERE user_id=b.user_id) AS topic_creator
FROM forum_replies a
LEFT JOIN forum_topics b ON a.topic_id=b.topic_id
LEFT JOIN users c ON a.user_id=c.user_id
// apply WHERE, ORDER, GROUP if needed
You miss the , after users.username..
SELECT forum_replies.reply_id, forum_replies.topic_id, forum_replies.user_id,
forum_replies.reply_text, forum_replies.reply_date, users.user_id, users.username,
forum_topics.topic_id,forum_topics.topic_title, forum_topics.topic_date

getting data from four tables with tight WHERE clause

i need help getting data from different tables and insert into other different table Here are the Queries
"SELECT commentID, date, comment, subject, parentID, aBUserID FROM comments WHERE status = 'APPROVED'"
"SELECT topicID, subForumID, aBUserID, lastPostID, views, replies, startDate FROM topic WHERE status = 'APPROVED' AND topicID = $parentid";
// $parentID need to be matched from above query parentID,
"SELECT userName FROM users WHERE aBUserID = $cmtaBUserID";
// $cmtaBUserID = aBUserID from first query
"SELECT userName FROM users WHERE aBUserID = $topicaBUserID";
//$topicaBUserID = aBUserID from second query
Last 2 queries are from same table but using different where clause
i used different inner join left join from solutions posted here but non of these worked for me stuck since last 2 weeks please help
PS data from all above Queries will be inserted to a single table i need these to be combined so i can have them all in one place
If you want to perform the operation in same query use 'OR'
"SELECT userName FROM users WHERE aBUserID = $cmtaBUserID OR aBUserID = $topicaBUserID";
Please try this
SELECT userName from users where aBUserID IN(SELECT aBUserID FROM comments WHERE status = 'APPROVED')
Couldn't test it but Maybe this is what you are looking for.
SELECT c.commentID, c.date, c.comment, c.subject, c.parentID, c.aBUserID,
t.topicID, t.subForumID, t.aBUserID, t.lastPostID, t.views, t.replies, t.startDate,
u.userName
FROM
comments c
left outer join topic t on t.topicID = c.parentID
left outer join users u on u.aBUserID = c.aBUserID and u.aBUserID = t.aBUserID
WHERE
c.status = 'APPROVED' and t.status = 'APPROVED';
try this:
SELECT
comment.[commentID],
comment.[date],
comment.[comment],
comment.[subject],
comment.[parentID],
comment.[aBUserID],
commentuser.[userName],
topic.[topicID],
topic.[subForumID],
topic.[aBUserID],
topic.[lastPostID],
topic.[views],
topic.[replies],
topic.[startDate],
topic.[userName]
FROM comments comment
LEFT OUTER JOIN users commentuser
ON commentuser.aBUserID = comment.[aBUserID]
LEFT OUTER JOIN
(
SELECT
t.[topicID],
t.[subForumID],
t.[aBUserID],
t.[lastPostID],
t.[views],
t.[replies],
t.[startDate],
u2.[userName] --user from users table joined to topics table
FROM topic t
LEFT OUTER JOIN users u
ON u.aBUserID = t.[aBUserID]
WHERE t.[status] = 'APPROVED'
) topic
ON topic.topicID = comment.parentID
WHERE comment.[status] = 'APPROVED'

Select From Inner join - PHP/MYSQL

I am struggling with a MYSQL query - I have 2 tables :
Table 1 (info) containing UID, first_name, last_name.
Table 2 (card) containing UID, pic .
What I am trying to do is get all results into an array:
WHERE UID IN '$ids' AND LEFT(last_name,1) = '$letter' ORDER BY last_name, first_name ASC
I figured an INNER JOIN so my current code is:
("SELECT UID, first_name, last_name, pic FROM
(SELECT info.first_name,info.last_name,card.pic FROM info
INNER JOIN card ON info.UID=card.UID)
WHERE LEFT(last_name,1) = '$letter' ORDER BY last_name, first_name ASC")
This is producing the following error though:
'Every derived table must have it's own alias'.
Am I going about this the right way with inner join, and how do I give the derived table an alias? Thanks in advance!
select b.UID, g.first_name, g.last_name, b.pic
from user_data.general_info g
inner join user_data.Bcards b on g.UID = b.UID
where LEFT(g.last_name, 1) = '$letter'
order by g.last_name, g.first_name asc
The inner query should be named.
SELECT users.UID, users.first_name, users.last_name, users.pic FROM
(SELECT info.first_name,info.last_name,card.pic FROM user_data.general_info
INNER JOIN user_data.Bcards ON general_info.UID=Bcards.UID) users
WHERE LEFT(users.last_name,1) = '$letter' ORDER BY users.last_name, users.first_name ASC

Problem with SQL statement in MySQL

I am running an SQL statement:
$sql = "SELECT pic, userid
FROM user_details
INNER JOIN wp_users ON user_details.userid
WHERE wp_users.id = user_details.userid
LIMIT $recordstart, $pagesize
";
The query is meant to select records from user_details and wp_users, but should only select if there is a match (wp_users.id = user_details.userid).
The target is for it to basically select from a table while retrieving a record (based on the id match) from the other table
The problem is that it shows duplicate records. How can I fix this problem?
You should put your JOIN condition after ON. Like this:
$sql="select pic, userid
from user_details
inner join wp_users on (wp_users.id=user_details.userid)
limit $recordstart, $pagesize";
But the real problem is that you have more then 1 record in user_details for each corresponding record in wp_users (or vise versa).
INNER JOIN is causing database to make a Cartesian product of user_details and wp_users records. Then result table is filtered by your current WHERE condition (wp_users.id=user_details.userid). Depends on your needs you can use GROUP BY or DISTINCT if you want to retrieve unique records only.
For example, if you need userid to be unique:
$sql="select pic, userid
from user_details
inner join wp_users on (wp_users.id=user_details.userid)
group by userid
limit $recordstart, $pagesize";
SELECT DISTINCT pic, userid
FROM user_details
INNER JOIN wp_users ON wp_users.id=user_details.userid
LIMIT $recordstart, $pagesize
Use DISTINCT to return only unique rows and complete your join condition in the ON section:
$sql="select distinct pic, userid
from user_details
inner join wp_users on wp_users.id=user_details.userid
limit $recordstart, $pagesize";
use GROUP BY , because it shows duplicate records if there is multiple records in second table per on one record in first table
You left out the = part of the ON by accident.
$sql="select pic, userid
from user_details
inner join wp_users on user_details.userid
= wp_users.id
where wp_users.id=user_details.userid
limit $recordstart, $pagesize";
I might try
$sql = "select pic, userid
from user_details
inner join wp_users on user_details.userid = wp_users.id
limit $recordstart, $pagesize";

Categories