order by with mysql INNER JOIN - php

i am fetching records from two tables with inner join, but i am not getting proper result.
Here is my code :
real_query = "SELECT DISTINCT user.id AS uid, user.name AS unm, user.profile_pic AS upic, user_message.message_by_id AS message_by_id
FROM user INNER JOIN
user_message
ON user.id = user_message.message_by_id
where (user_message.message_to_id = '".$sess."' AND
user_message.receiver_deleted = 'No')
group by user_message.id, user_message.sent_on
order by user_message.id DESC ";
The problem is, i want to echo those user names from user table, order by user_message.id but i am not getting the result as i want.

Related

Get the last records from 3 tables

My tables
$sql="SELECT *
FROM addresses
LEFT JOIN users ON address_id = user_id
LEFT JOIN notes ON note_id = user_id
ORDER BY id DESC
LIMIT 1";
This is my SQL query, my task is to show the last records from 3 tables, but the table is blank, I don't know why,thanks in advance people :)
I guess the problem is coming from the ORDER BY id DESC .
Indeed, you have no column so called id.
You should probably remove this clause, in order to make your code work.
If you want to take the last records anyway, you can put an ORDER BY address_id DESC which will do the job !
The code directly edited :
$sql="SELECT *
FROM addresses
LEFT JOIN users ON address_id = user_id
LEFT JOIN notes ON note_id = user_id
ORDER BY adress_id DESC
LIMIT 1";
This may work:
SELECT a.address_id, u.user_id, n.note_id
FROM addresses a
LEFT JOIN users_addresses ua ON ua.ua_address_id = a.address_id
LEFT JOIN users u ON u.user_id = ua.ua_user_id
LEFT JOIN notes n ON n.note_user_id = u.user_id
ORDER BY a.address_id DESC
LIMIT 1
Here is the query to get all data from all the tables, not sure what do you mean last records from 3 tables, I can see four tables there:
SELECT *
FROM `addresses`
LEFT JOIN `users_addresses` ON `users_addresses`.`ua_address_id` = `addresses`.`address_id`
LEFT JOIN `users` ON `users`.`user_id` = `users_addresses`.`ua_user_id`
LEFT JOIN `notes` ON `notes`.`note_user_id` = `users`.`user_id`;

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;

MySql : left join users table with transactions table return several rows

I have 2 tables : users and paypal_transactions
For each user, we have an id (named user_id in paypal_transactions table)
A user may have several paypal_transactions. Relation one to many
I need to grab the latest transaction id (ordered by date_dt DESC) when I do my query
My current query :
SELECT `Transaction`.*, `User`.*, `Tipster`.`username`
FROM `pronostics_framework`.`users` AS `User`
LEFT JOIN `pronostics_framework`.`users` AS `Tipster` ON (`User`.`tipster_id` = `Tipster`.`id`)
LEFT JOIN `pronostics_framework`.`paypal_transactions` AS `Transaction` ON (`User`.`id` = `Transaction`.`user_id`)
ORDER BY `User`.`id` DESC
LIMIT 500
Currently with one transaction per user, it works fine. BTW with many transactions I still get the 1st entry from paypal_transactions table (the oldest, but I want the latest from now).
I did many tries, without success.
Thanks for your help !
Here you go:
SELECT `Transaction`.*, `User`.*, `Tipster`.`username`
FROM `pronostics_framework`.`users` AS `User`
LEFT JOIN `pronostics_framework`.`users` AS `Tipster` ON (`User`.`tipster_id` = `Tipster`.`id`)
LEFT JOIN (SELECT user_id, MAX(date_dt) AS max_date
FROM `pronostics_framework`.paypal_transactions
GROUP BY user_id) AS max_trans
ON User.id = max_trans.user_id
LEFT JOIN `pronostics_framework`.`paypal_transactions` AS `Transaction`
ON (max_trans.user_id = `Transaction`.`user_id` AND max_trans.max_date = Transation.date_dt)
ORDER BY `User`.`id` DESC
LIMIT 500
Another way, based on the first query in the first answer at Retrieving the last record in each group:
SELECT `Transaction`.*, `User`.*, `Tipster`.`username`
FROM `pronostics_framework`.`users` AS `User`
LEFT JOIN `pronostics_framework`.`users` AS `Tipster` ON (`User`.`tipster_id` = `Tipster`.`id`)
LEFT JOIN `pronostics_framework`.`paypal_transactions` AS `Transaction` ON Transaction.user_id = User.id
LEFT JOIN `pronostics_framework`.paypal_transactions AS Transactions1
ON Transactions1.user_id = Transactions.user_id AND Transactions1.user_id > Transactions.user_id
WHERE Transactions1.user_id IS NULL
ORDER BY `User`.`id` DESC
LIMIT 500

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";

fetching data from the inner join query

hello please help me out regarding this query ,I am fetching data from different table The problem i am facing is that in the table there are similar colum name like employee have and user has also name . The query work perfectly but i am wordering about how i can display this data as
$data["employee.name"]
$data["user.name"]
here is the query:
SELECT task.employee_id , task.user_id , task.service_id, user.name,
user.pic_path , employee.name ,employee.pic_path
FROM task
INNER JOIN employee ON employee.pno = task.employee_id
INNER JOIN user ON user.pno = task.user_id
INNER JOIN service ON service.service_id = task.service_id ";
SELECT user.name AS username, employee.name AS employeename
You get the point.
There are two steps:
You need to define a column alias for at least one of the two columns in the SQL statement:
SELECT t.employee_id,
t.user_id,
t.service_id,
u.name AS user_name,
u.pic_path,
e.name AS employee_name,
e.pic_path
FROM TASK t
JOIN EMPLOYEE e ON e.pno = t.employee_id
JOIN USER u ON ur.pno = t.user_id
JOIN SERVICE s ON s.service_id = t.service_id
Then you need to update the PHP logic to use the column aliases:
$empname = $data["employee_name"];
$username = $data["user_name"];

Categories