Select Latest Records from Customer table - php

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

Related

get notification from single table and join with multiple tables and result as single array

i have a table like
and select all notifications from this table and join with below mentioned tables
tbl_notice
tbl_task
tbl_assignment
and i am trying to fetch datas from tables like
$sql = "select * from(
(
SELECT a.refer_id as id ,a.refer_tbl,b.task_name as name FROM tbl_notification as a
LEFT JOIN tbl_task as b ON (a.refer_id= b.task_id )ORDER BY a.created_date desc
) UNION
(
SELECT c.refer_id as id ,c.refer_tbl, d.notice_title as name FROM tbl_notification as c
LEFT JOIN tbl_notice as d ON (c.refer_id = d.notice_id)ORDER BY c.created_date desc
) UNION
(
SELECT e.refer_id as id,e.refer_tbl, f.title as name FROM tbl_notification as e
LEFT JOIN tbl_assignment as f ON (e.refer_id = f.assignment_id) ORDER BY e.created_date desc
))";
$query= $this->db->query($sql)->get();
the result shows the data from first table where join (in case tbl_task) other tbl_notice and tbl_assignment details are null result demo like this
Please help me to correct this.....
You're performing a LEFT (OUTER) JOIN when it sounds like you're needing a plain old (INNER) JOIN. Try this:
SELECT a.refer_id as id ,a.refer_tbl,b.task_name as name FROM tbl_notification as a
JOIN tbl_task as b ON (a.refer_id= b.task_id )ORDER BY a.created_date desc
UNION
SELECT c.refer_id as id ,c.refer_tbl, d.notice_title as name FROM tbl_notification as c
JOIN tbl_notice as d ON (c.refer_id = d.notice_id)ORDER BY c.created_date desc
UNION
SELECT e.refer_id as id,e.refer_tbl, f.title as name FROM tbl_notification as e
JOIN tbl_assignment as f ON (e.refer_id = f.assignment_id) ORDER BY e.created_date desc
i got the answer with below attach code
select * from ( SELECT a.refer_id as id ,a.refer_tbl,a.created_date,b.task_name as name FROM tbl_notification as a JOIN tbl_task as b ON (a.refer_id= b.task_id AND a.refer_tbl="tbl_task")
UNION SELECT c.refer_id as id ,c.refer_tbl,c.created_date, d.notice_title as name FROM tbl_notification as c JOIN tbl_notice as d ON (c.refer_id = d.notice_id AND c.refer_tbl="tbl_notice")
UNION SELECT e.refer_id as id,e.refer_tbl,e.created_date, f.title as name FROM tbl_notification as e JOIN tbl_assignment as f ON (e.refer_id = f.assignment_id AND e.refer_tbl="tbl_assignment") ) tbl_notification ORDER BY `tbl_notification`.`created_date` desc
Thaks for helping me.....

LEFT JOIN Query With Condition

I have a query that LEFT JOINS another table to group rows. I am trying to only select records from the first table "googleimage" where the user_id is a certain number. (user_id is a column in "googleimage" table.
SELECT g.*
FROM googleimage g
LEFT JOIN (SELECT image_id, COUNT(*) AS cnt
FROM googleimagefound WHERE status = 0
GROUP BY image_id) gf ON gf.image_id = g.id
ORDER BY COALESCE(cnt, 0) DESC");
I have tried adding a new ON statement beiside the
gf ON gf.image_id = g.id
I have also tried changing
SELECT g.*
FROM googleimage g
to
SELECT g.*
FROM googleimage WHERE user_id = 1 g
but none of them seem to work, any help will be appriciated
Try changing you query a bit like below
SELECT g.*
FROM googleimage g
LEFT JOIN (SELECT image_id, COUNT(*) AS cnt
FROM googleimagefound WHERE status = 0
GROUP BY image_id) gf ON gf.image_id = g.id
WHERE g.user_id = 1 <-- add this condition
ORDER BY COALESCE(gf.cnt, 0) DESC;

MySQL Query - alternative

My query is too long (3-4s). Any idea's how make this faster?
SELECT u.id AS id_uzytkownika,
u.login,
u.ranga,
u.online_light AS online,
(SELECT MAX(id)
FROM uzytkownicy_zdjecia
WHERE id_uzytkownika = u.id
AND prywatna =0) AS id_fotki,
(SELECT fotka
FROM uzytkownicy_zdjecia
WHERE id = id_fotki) AS fotka ,
(SELECT srednia_ocen
FROM uzytkownicy_zdjecia
WHERE id = id_fotki) AS srednia_ocen,
(SELECT ile_ocen
FROM uzytkownicy_zdjecia
WHERE id = id_fotki) AS ile_ocen
FROM uzytkownicy u
WHERE u.foto =1
AND u.plec = "mezczyzna"
ORDER BY srednia_ocen DESC,
ile_ocen DESC,
id_fotki DESC LIMIT 42
Could you maybe explain your table structure and what this query is about? Since the column names are not in English, most readers will probably have a problem understanding what you are trying to do here...
In general, it looks like you have a LOT of nested SELECTS here to the same table - is there any special reason for that?
Try this:
SELECT u.id AS id_uzytkownika,
u.login,
u.ranga,
u.online_light AS online,
(SELECT MAX(id)
FROM uzytkownicy_zdjecia
WHERE id_uzytkownika = u.id
AND prywatna =0) AS id_fotki,
z.fotka,
z.srednia_ocen,
z.ile_ocen
FROM uzytkownicy u
JOIN uzytkownicy_zdjecia z
ON u.id_fotki = z.id
WHERE u.foto =1
AND u.plec = "mezczyzna"
ORDER BY srednia_ocen DESC,
ile_ocen DESC,
id_fotki DESC LIMIT 42
I've substituted 3 subqueries with a JOIN. The first subquery has a different condition, if you can merge that condition withn others queries you can remove that.
Pay attention You have named id_fokta the result of subquery, but the same name has the primary key of uzytkownicy table
You don't have to query the same table with the same criteria again and again. Replace your sub-selects with a simple join. A LEFT JOIN, if it is possible that no matching record exists.
SELECT
u.id AS id_uzytkownika,
u.login,
u.ranga,
u.online_light AS online,
(
SELECT MAX(id)
FROM uzytkownicy_zdjecia
WHERE id_uzytkownika = u.id
AND prywatna =0
) AS id_fotki,
uz.fotka,
uz.srednia_ocen,
uz.ile_ocen
FROM uzytkownicy u
LEFT JOIN uzytkownicy_zdjecia uz ON uz.id = u.id_fotki
WHERE u.foto = 1
AND u.plec = "mezczyzna"
ORDER BY u.srednia_ocen DESC,
u.ile_ocen DESC,
u.id_fotki DESC LIMIT 42
An alternative way to write the query above is to aggregate first and then join:
SELECT
u.id AS id_uzytkownika,
u.login,
u.ranga,
u.online_light AS online,
uzz.id_fotki,
uz.fotka,
uz.srednia_ocen,
uz.ile_ocen
FROM uzytkownicy u
LEFT JOIN uzytkownicy_zdjecia uz ON uz.id = u.id_fotki
LEFT JOIN
(
SELECT id_uzytkownika, MAX(id) AS id_fotki
FROM uzytkownicy_zdjecia
WHERE prywatna =0
GROUP BY id_uzytkownika
) uzz ON uzz.id_uzytkownika = u.id
WHERE u.foto = 1
AND u.plec = "mezczyzna"
ORDER BY u.srednia_ocen DESC,
u.ile_ocen DESC,
u.id_fotki DESC LIMIT 42
By the way: What is "mezczyzna"? A string? Then this should be single quotes.

How to get total count from query?

this is my query
SELECT a.id,
a.venue_id,
a.user_id,
m1.profilenam AS user_profilename,
m1.photo_thumb AS user_photo_thumb,
m2.profilenam AS venue_profilename,
m2.photo_thumb AS venue_photo_thumb
FROM announce_arrival AS a
INNER JOIN members AS m1
ON a.user_id = m1.mem_id
INNER JOIN members AS m2
ON a.venue_id = m2.mem_id
GROUP BY a.venue_id, a.user_id
LIMIT 0,10
ORDER BY date DESC,
time DESC
How can i use count(*) on this query,i use like this
SELECT DISTINCT COUNT(*)
FROM announce_arrival AS a
INNER JOIN members as m1 ON (a.user_id = m1.mem_id)
INNER JOIN members as m2 ON (a.venue_id= m2.mem_id)
GROUP BY a.venue_id, a.user_id LIMIT 0,10 ORDER BY date DESC,time DESC;
but its showing
COUNT(*)
7
3
1
i want total count .
You can wrap your query into select count(*), like this:
SELECT COUNT(*) FROM
(SELECT a.id,a.venue_id, a.user_id, m1.profilenam as
user_profilename,m1.photo_thumb AS user_photo_thumb,m2.profilenam AS
venue_profilename, m2.photo_thumb AS venue_photo_thumb FROM announce_arrival
AS a INNER JOIN members as m1 ON (a.user_id = m1.mem_id) INNER JOIN members
as m2 ON (a.venue_id= m2.mem_id) GROUP BY a.venue_id, a.user_id)
You can wrap your query in another SELECT:
SELECT COUNT(*) AS total FROM (
SELECT DISTINCT COUNT(*)
FROM announce_arrival AS a
INNER JOIN members as m1 ON (a.user_id = m1.mem_id)
INNER JOIN members as m2 ON (a.venue_id= m2.mem_id)
GROUP BY a.venue_id, a.user_id LIMIT 0,10 ORDER BY date DESC,time DESC) AS t
or if you want sum of all DISTINCT COUNT(*) try:
SELECT SUM(cnt) AS total FROM (
SELECT DISTINCT COUNT(*) AS cnt
FROM announce_arrival AS a
INNER JOIN members as m1 ON (a.user_id = m1.mem_id)
INNER JOIN members as m2 ON (a.venue_id= m2.mem_id)
GROUP BY a.venue_id, a.user_id LIMIT 0,10 ORDER BY date DESC,time DESC) AS t
From docs about found_rows():
To obtain this row count, include a
SQL_CALC_FOUND_ROWS option in the
SELECT statement, and then invoke
FOUND_ROWS() afterward:
mysql> SELECT SQL_CALC_FOUND_ROWS * FROM tbl_name WHERE id > 100 LIMIT 10;
mysql> SELECT FOUND_ROWS();
The second SELECT returns a number
indicating how many rows the first
SELECT would have returned had it been
written without the LIMIT clause.
I.e., add SQL_CALC_FOUND_ROWS after SELECT in your first query and replace second query with SELECT FOUND_ROWS().
Get rid of GROUP BY, LIMIT and ORDER. They are useless and don't make sense (especially LIMIT) if you need a total count. DISTINCT doesn't make sense either.
SELECT COUNT(*)
FROM announce_arrival AS a
INNER JOIN members as m1 ON (a.user_id = m1.mem_id)
INNER JOIN members as m2 ON (a.venue_id= m2.mem_id)
Try this
SELECT COUNT(a.user_id)
FROM announce_arrival AS a
INNER JOIN members as m1 ON (a.user_id = m1.mem_id)
INNER JOIN members as m2 ON (a.venue_id= m2.mem_id)
GROUP BY a.user_id LIMIT 0,10;
If you are using count no need to give order by

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