hide duplicate row sql phpmyadmin - mysql

i have problem about duplicate row.
And my purpose like this:
Can you help me sir what the query to do like that?
The code I have been using is as follows:
select a.id_task1, b.id_task, b.task, b.cycle_sequence_number, c.cycle_sequence_number FROM task_table a LEFT OUTER JOIN billing_cycle b ON (a.id_task1=b.id_task) LEFT OUTER JOIN header c ON (b.cycle_sequence_number = c.cycle_sequence_number) where a.id_task1< 23 order by b.cycle_sequence_number DESC;
Regards,
-Tri-
Sorry sir, i forgot post my code:
select a.id_task1, b.id_task, b.task, b.cycle_sequence_number, c.cycle_sequence_number
FROM task_table a
LEFT OUTER JOIN billing_cycle b ON (a.id_task1=b.id_task)
LEFT OUTER JOIN header c ON (b.cycle_sequence_number = c.cycle_sequence_number)
where a.id_task1< 23
order by b.cycle_sequence_number DESC;

In relational databases the results of your queries are relations/tables ('relation' and 'table' are synonymous expressions). So, let's name your query and use group bys:
select id_task1, id_task, task, max(b.cycle_sequence_number) as b_cycle_sequence_number, max(c.cycle_sequence_number) as c_cycle_sequence_number
from (...) t
group by id_task1, id_task, task
order by b_cycle_sequence_number
You should interpret b.cycle_sequence_number as b_cycle_sequence_number and c.cycle_sequence_number as c_cycle_sequence_number to avoid duplicate column names. I'm sorry for not rewriting your actual code, but I refuse to type in your code from a picture.
Edit: The real code is:
select a.id_task1, b.id_task, b.task, b.cycle_sequence_number, c.cycle_sequence_number FROM task_table a LEFT OUTER JOIN billing_cycle b ON (a.id_task1=b.id_task) LEFT OUTER JOIN header c ON (b.cycle_sequence_number = c.cycle_sequence_number) where a.id_task1< 23 order by b.cycle_sequence_number DESC;
Suggestion:
SELECT id_task1, id_task, task, max(b.cycle_sequence_number) as b_cycle_sequence_number, max(c.cycle_sequence_number) as c_cycle_sequence_number
FROM (SELECT a.id_task1, b.id_task, b.task, b.cycle_sequence_number as b_cycle_sequence_number, c.cycle_sequence_number as c_cycle_sequence_number
FROM task_table a
LEFT OUTER JOIN billing_cycle b
ON (a.id_task1=b.id_task)
LEFT OUTER JOIN header c
ON (b.cycle_sequence_number = c.cycle_sequence_number) where a.id_task1< 23) t
GROUP BY id_task1, id_task, task
ORDER BY b_cycle_sequence_number

Related

Adjusting a SELECT statement containing a LEFT OUTER JOIN to an UPDATE statement from the same table- is it possible?

As a newby, I've hunted high, and low to find a solution to my problem. Hoping someone can shed some light on a solution.
I have a SELECT statement that spits out Reports as desired. What I'd like to do is have an UPDATE statement that updates my table column named report with the resulting number. Any suggestions would be greatly appreciated.
SELECT
l.id,
l.plate,
COUNT(*) AS Reports
FROM
coh_items AS l
LEFT OUTER JOIN coh_items AS r
ON
l.id >= r.id AND l.plate = r.plate
GROUP BY
l.id,
l.plate
;
Is this what you want?
update coh_items i left join
(select i.id, i.plate, count(*) as reports
from coh_items i left join
coh_items i2
on i.id >= i2.id and i.plate = i2.plate
group by i.id, i.place
) i2
using (id, plate)
set i.reports = i2.reports;
The left join seems unnecessary because any given row is always going to match itself (assuming the comparison columns are not NULL).

SQL Join Issues Microsoft Query

I've been trying to write some code in SQL, but it keeps coming up with a syntax error regarding the join, and I can't work out why.
SELECT `COUNTRY$`.country_name, `PARTNER$`.partner_name, count(member_id)
FROM `Member$`
Left Join `COUNTRY$`
ON `MEMBER$`.country_id=`COUNTRY$`.country_id
lEFT jOIN `PARTNER$`
on `MEMBER$`.partner_ID = `PARTNER$`.partner_ID
Group By country_name,Partner_name
Any help would be appreciated.
May have something to do with how your table names are in 'thisFormat$'. Also you did not specify which table member_id was coming and group by also doesn't specify which table country_name, partner_name was from.
Try putting aliases on the tablenames and see if that eliminates the problem
SELECT c.country_name, p.partner_name, count(m.member_id)
FROM `Member$` m
left join `COUNTRY$` c on c.country_id = m.country_id
left join `PARTNER$` p on p.partner_id = m.partner_id
GROUP BY c.country_name, p.partner_name

How to optimize a query that's running too slow (LEFT JOIN)

My query is running too slow.....
any ways to speed it up?
SELECT SQL_CALC_FOUND_ROWS a.*, b.*, c.*, d.*, e.*, f1.*, st6.*
FROM tbl_a a
LEFT JOIN tbl_b b ON a.m_id = b.m_id
LEFT JOIN tbl_c c ON a.ms_id = c.ms_id
LEFT JOIN tbl_d d ON a.gd_id = d.gd_id
LEFT JOIN tbl_e e ON a.sd_id = e.sd_id
LEFT JOIN tbl_f f1 ON a.tp_id = f1.tp_id
LEFT JOIN
(
SELECT g.*, GROUP_CONCAT(f2.tp_name SEPARATOR ',') tp_name, f2.tp AS tp
FROM tbl_g g
LEFT JOIN tbl_f f2 ON g.tp_id = f2.tp_id
GROUP BY s_id
)st6 ON st6.s_id = a.s_id
Do not use LEFT unless you have a specific reason for it. I see no hint of such here.
A query like this is best optimized if it can perform the subquery first. However, 'LEFT' is a hint that it cannot be. Remove all the LEFTs and see if it is fast enough and gives the "right" answer.
Check to see that there is a PRIMARY KEY or INDEX on gd_id in both of the indicated tables in the following. (And do likewise for other JOINs.)
JOIN tbl_d d ON a.gd_id = d.gd_id
If you still need help, provide SHOW CREATE TABLE for each table. And EXPLAIN SELECT ...;.

Select only one record from many duplicates

Hello im using the below query to select some elements from the db.
SELECT *,c.slug AS pslug,d.slug AS catslug
FROM uhhu_virtuemart_products as a
LEFT JOIN uhhu_virtuemart_products_el_gr as d
on a.virtuemart_product_id=d.virtuemart_product_id
LEFT JOIN uhhu_virtuemart_product_categories as b
ON a.virtuemart_product_id = b.virtuemart_product_id
LEFT JOIN uhhu_virtuemart_categories_el_gr AS c
ON b.virtuemart_category_id=c.virtuemart_category_id
WHERE a.virtuemart_product_id = 508
The problem is that a product can have many categories , so this query returns me 3 rows, while i need to take always 1 row for output.
virtuemart_product_categories:
What i want is, when it check for the category here :
LEFT JOIN uhhu_virtuemart_product_categories as b
ON a.virtuemart_product_id = b.virtuemart_product_id
put a LIMIT 1 statement
I tried to use a nested select like this:
SELECT *,c.slug AS pslug,d.slug AS catslug
FROM uhhu_virtuemart_products as a
WHERE uhhu_virtuemart_product_id =
(SELECT virtuemart_product_id
from virtuemart_product_categories
where virtuemart_product_id=508 LIMIT 1)
LEFT JOIN uhhu_virtuemart_categories_el_gr AS c
ON a.virtuemart_category_id=c.virtuemart_category_id
LEFT JOIN uhhu_virtuemart_categories_el_gr as d
on c.virtuemart_category_id=d.virtuemart_category_id
And Like this:
SELECT *,c.slug AS pslug,d.slug AS catslug
FROM uhhu_virtuemart_product_categories as a
LEFT JOIN uhhu_virtuemart_products_el_gr as d
on a.virtuemart_product_id=d.virtuemart_product_id
LEFT JOIN uhhu_virtuemart_categories_el_gr AS c
ON a.virtuemart_category_id=c.virtuemart_category_id
WHERE a.virtuemart_product_id =(
SELECT virtuemart_product_id
from uhhu_virtuemart_product_categories as f
where f.virtuemart_product_id=508
LIMIT 1)
I tried also use SELECT distinct but it didnt work too.I know only the basics of SQL and couldnt find something similar to help me solve this, so i would appreciate your help.
Hope i was clear enough.

MySQL Select ALL from Left Join

This query does not work. I want all the results from a LEFT JOIN where something is something. This is my exact code:
SELECT * FROM `swarovski_zones` WHERE `siteid`='200'
LEFT JOIN `trafficviews` ON `swarovski`.`id`=`trafficviews`.`adid`
swarovski_zones table is siteid 200
trafficviews table is adid 200
The 200 is the linking variable between the tables. I want everything from both tables where the ID is 200.
The query doesn't work because the syntax is incorrect. It should be:
select
from
join
on
where
group by
having
order by
limit
Giving you:
select *
from `swarovski_zones`
left join `trafficviews`
on `swarovski`.`id` = `trafficviews`.`adid`
where `siteid` = '200'
Also is siteid meant to be a string and not an integer?
I'm probably going to regret providing the list above...
Limit! I forgot Limit; the full syntax list is here
The problem here is that elements in your right table (trafficviews) might not have a correspondant row in your left table (swarovski_zones). So the left join will get all elements from the left and might leave out some elements from the right.
To solve this, you need an outer join. Your problem is that MySQL does not support outer joins :) This is solved in the following general way:
SELECT * FROM a LEFT JOIN b ON a.id = b.id
UNION ALL
SELECT * FROM a RIGHT JOIN b ON a.id = b.id WHERE a.id IS NULL;
Applied to your question this should be something like:
SELECT * FROM swarovski_zones s
LEFT JOIN trafficviews ON s.id = t.adid
UNION ALL
SELECT * FROM swarovski_zones s
RIGHT JOIN trafficviews ON s.id = t.adid WHERE s.id IS NULL
WHERE s.siteid = 200 or t.adid = 200
Give it a try.
User full outer join
SELECT * FROM `swarovski_zones` WHERE `siteid`='200'
FULL OUTER JOIN `trafficviews` ON `swarovski`.`id`=`trafficviews`.`adid`