Trying my first sql left join - isn't working - php

HiI'm trying to join two database table and extract the rows to php variables. But it's my first join function, and I'm not getting any values returned to my form.
SELECT s.supplierid,
s.name,
s.address,
s.zipCode,
s.cityName,
s.region,
s.country,
s.phone,
s.fax,
s.contactMame,
s.contactTitle,
s.contactPhone,
s.contactEmail,
s.notes,
s.employeeid,
e.employeeid as emp_id,
e.name as emp_name
FROM suppliers s
LEFT JOIN employees e
ON s.employeeid=e.employeeid
WHERE supplierid=? limit 0,1
ORDER BY name DESC
LIMIT :from_record_num, :records_per_page";
And then I assign them to variables like this
$name = $row['name'];
$address = $row['address'];
$emp_name = $row['emp_name'];
I need to use it another time in my code, but to extract all rows from the table. Is there a way to use
SELECT *
in a left join?

Try something like this:-
( assumes pagination vars $from_record_num and $records_per_page )
$sql="select
s.supplierid,
s.name,
s.address,
s.zipcode,
s.cityname,
s.region,
s.country,
s.phone,
s.fax,
s.contactmame,
s.contacttitle,
s.contactphone,
s.contactemail,
s.notes,
s.employeeid,
e.employeeid as 'emp_id',
e.name as 'emp_name'
from suppliers s
left join employees e on s.employeeid=e.employeeid
where s.supplierid=?
order by s.name desc
limit {$from_record_num}, {$records_per_page}";

Related

Mysql query not giving exact records

I want the unique records against each user and product id. So my query is working fine but a small issue is, I am not getting the exact value of columns against the order id.
select max(o.id) as order_id, u.email,oi.product_id, DATE_FORMAT(FROM_UNIXTIME(o.paid_time), '%Y-%m-%d') as paid_times
from `order` o
join `users` u on u.id = o.user_id
join order_item oi on oi.order_id = o.id
where oi.product_id in (1212,1213)
group by u.email, oi.product_id
order by o.id desc;
Try - 2
select max(o.id) as order_id, u.email,oi.product_id, DATE_FORMAT(FROM_UNIXTIME(o.paid_time), '%Y-%m-%d') as paid_times
from `order` o
join `users` u on u.id = o.user_id
join order_item oi on oi.order_id = o.id
where oi.product_id in (1212,1213) and o.id IN (SELECT max(id) FROM `order`)
group by u.email, oi.product_id
order by o.id desc;
I get the max order id, which is fine. but the paid_time is not of this order id.
Current Results:
Desired Results:
This is very common type of error. for this first you need to get the max order id than use it in where condition as taking max with select and others columns mysql gives you max order id and any row data which comes first not the data related to max id.
Understand below example
SELECT name
FROM table
WHERE id=(
SELECT max(id) FROM table
)
above will give you data of max(id) from table
SELECT name , max(id)
FROM table
the above query will display Maximum id but not the matching name
http://www.plus2net.com/sql_tutorial/sql_max.php

SELECT LEFT JOIN with LIMIT

I have a problem putting a limit on the number of rows from my Jokes table.
This is my query working, getting all rows:
SELECT Jokes.ID, Categories.CategoryName, Jokes.CategoryID, Jokes.JokeText
FROM Jokes
LEFT JOIN Categories
ON Jokes.CategoryID = Categories.ID
ORDER BY Jokes.ID DESC
Would it be something like?
SELECT Jokes.ID, Categories.CategoryName, Jokes.CategoryID, Jokes.JokeText
FROM (
SELECT * FROM Jokes ORDER BY ID DESC Limit 0,40)
AS a
LEFT JOIN Categories
AS b
ON a.CategoryID = b.ID
why not using
SELECT Jokes.ID, Categories.CategoryName, Jokes.CategoryID, Jokes.JokeText
FROM Jokes
LEFT JOIN Categories
ON Jokes.CategoryID = Categories.ID
ORDER BY Jokes.ID DESC
Limit 0,40

Select Latest Records from Customer table

I am trying to fetch record of a customer's latest transaction. The query I am trying is this:
SELECT
food_rate,
ambiance_rate,
service_rate,
cost_rate
FROM
tbl_transaction t
INNER JOIN tbl_feedback f
ON t.fid = f.fid
WHERE date_time IN
(SELECT
MAX(date_time)
WHERE c_id = 1)
The output will be a single row only. But it is giving syntax error.
Try this:
SELECT
food_rate,
ambiance_rate,
service_rate,
cost_rate
FROM
tbl_transaction t
INNER JOIN tbl_feedback f
ON t.fid = f.fid
WHERE c_id = 1
ORDER BY date_time DESC
LIMIT 1;
//you have not written table name in your subquery, i have rewritten the query
SELECT
food_rate,
ambiance_rate,
service_rate,
cost_rate
FROM
tbl_transaction t
INNER JOIN tbl_feedback f
ON t.fid = f.fid
WHERE date_time IN
(SELECT
MAX(date_time) from tbl_transaction
WHERE c_id = 1)
Try this
SELECT food_rate, ambiance_rate, service_rate, cost_rate
from tbl_transaction t
inner join tbl_feedback f ON t.fid=f.fid
where c_id=1
order by date_time desc
limit 1

MySQL Selecting a complex sub query as a field

I currently have:
SELECT tbl_review.*, users.first_name, users.last_name, (
SELECT order_ns.tran_date
FROM order_ns
LEFT JOIN product_2_order_ns.external_order_id = order_ns.order_id
WHERE product_2_order_ns.bkfno IN :id
ORDER BY order_ns.trandate ASC
LIMIT 1
) as purchase_date
FROM tbl_review
LEFT JOIN users ON users.sequal_user_id = tbl_review.user_id
WHERE tbl_review.product_id IN :id AND tbl_review.approved = 1
Which, in its sub query, selects an order the user has which has a product in question (defined in :id) get the the oldest transaction date on file for one of the found orders.
I would really like to keep this to one call of the database (don't really want to call again for each returned user for just one field, or even do a range query of all users) but obviously this particular query isn't working.
What can I do, if anything, to get this working?
I cannot make the sub query into a join since they are two distinct pieces of data, the sub query needs to return detail for each row in the main query.
I think you just want a correlated subquery. It is unclear exactly what the relationship is between the inner query and the outer one. My guess is that it is on users and orders:
SELECT tbl_review.*, users.first_name, users.last_name,
(SELECT order_ns.tran_date
FROM order_ns LEFT JOIN
product_2_order_ns
on product_2_order_ns.external_order_id = order_ns.order_id and
product_2_order_ns.bkfno = tbl_review.product_id and
WHERE order_ns.user_id = tbl_review.user_id
ORDER BY order_ns.trandate ASC
LIMIT 1
) as purchase_date
FROM tbl_review LEFT JOIN
users
ON users.sequal_user_id = tbl_review.user_id
WHERE tbl_review.product_id IN :id AND tbl_review.approved = 1;
EDIT:
Oh, the inner query has no relationship to the outer query. Then it is easier. Move it to the from clause using cross join:
SELECT tbl_review.*, users.first_name, users.last_name,
innerquery.tran_date as purchase_date
FROM tbl_review LEFT JOIN
users
ON users.sequal_user_id = tbl_review.user_id cross join
(SELECT order_ns.tran_date
FROM order_ns LEFT JOIN
product_2_order_ns
on product_2_order_ns.external_order_id = order_ns.order_id
WHERE product_2_order_ns.bkfno IN :id
ORDER BY order_ns.trandate ASC
LIMIT 1
) innerquery
WHERE tbl_review.product_id IN :id AND tbl_review.approved = 1;
#Gordons answer is really close but I wanted it to return even if no data was found for tran_date so I changed my query to:
SELECT tbl_review.*, users.first_name, users.last_name, order_ns.tran_date
FROM tbl_review
LEFT JOIN users ON users.sequal_user_id = tbl_review.user_id
LEFT JOIN order_ns ON order_ns.order_id = (
SELECT order_ns.order_id
FROM order_ns
LEFT JOIN product_2_order_ns on product_2_order_ns.external_order_id = order_ns.order_id
WHERE product_2_order_ns.bkfno IN :id
ORDER BY order_ns.tran_date ASC
LIMIT 1
)
WHERE tbl_review.product_id IN :id AND tbl_review.approved = 1;
This will return the distinct data of tran_date irrespective of whether it is found or not.

Help with a joined query (MySQL)

Hello can anybody see why this query fails?
SELECT A.idAd, A.ads_in_Cat, A.title, A.currency, A.price,
A.in_dpt, A.description, A.featured FROM ads A
LEFT JOIN featured F ON F.ad = A.idAd
INNER JOIN dept D ON D.id_dept = A.in_dpt
INNER JOIN sub_cat_ad S ON S.id_sub_cat = A.ads_in_Cat
INNER JOIN cat_ad C ON C.idCat_ad = S.from_cat_ad
ORDER BY A.featured DESC LIMIT :limit, :offset
But this one works:
SELECT *FROM ads
LEFT JOIN featured ON featured.ad = ads.idAd
INNER JOIN dept ON dept.id_dept = ads.in_dpt
INNER JOIN sub_cat_ad ON id_sub_cat = ads.ads_in_Cat
INNER JOIN cat_ad ON idCat_ad = sub_cat_ad.from_cat_ad
ORDER BY featured DESC LIMIT :limit, :offset
In the first one, I don't want all columns from the table "ads", the query returns only columns till ...FROM ads A.
If you specify to only select fields from A that's what you get: Only fields from A.
If you want fields from other tables too you have to specify them as well.
SELECT
A.idAd, A.ads_in_Cat, A.title, A.currency, A.price, A.in_dpt, A.description, A.featured,
F.*,
D.*,
S.*,
C.*
FROM ads A
LEFT JOIN featured F ON F.ad = A.idAd
INNER JOIN dept D ON D.id_dept = A.in_dpt
INNER JOIN sub_cat_ad S ON S.id_sub_cat = A.ads_in_Cat
INNER JOIN cat_ad C ON C.idCat_ad = S.from_cat_ad
ORDER BY A.featured DESC LIMIT :limit, :offset

Categories