Inner Join between three tables - mysql

I'm using the following query:
select a.idclientecrm, max(c.falta) from clientescrmporlistadeclientescrm a
inner join clientescrmporlistadeclientescrm b on (a.idclientecrm=b.idclientecrm and a.idlistadeclientescrm = 58)
inner join tareas c on a.idclientecrm=c.idclientecrm
where b.idlistadeclientescrm = 70
But I'm not getting the result I want. I know I'm doing something wrong but I don't know what.
I want the outcome of the first inner join (aprox 22k rows) but I need to join the result to the "tareas" table and get the max date from there.
I want to use max because for every idclientecrm, there's more than one row that matches in the "tareas" table and I need the last recorded result.
If I left out something let me know.
Thnx in advance!

You probably want to move the "58" condition to the WHERE clause and group on a.idclientecrm:
select a.idclientecrm, max(c.falta)
from clientescrmporlistadeclientescrm a
inner join clientescrmporlistadeclientescrm b
on a.idclientecrm = b.idclientecrm
inner join tareas c
on a.idclientecrm = c.idclientecrm
where b.idlistadeclientescrm = 70
and a.idlistadeclientescrm = 58
group by a.idclientecrm

Related

PHP MySQL: Join 3 tables but

I have 3 tables, errorcode_table, description_table, and customer_table.
The query below will display all records that are in the errorcode_table and I have an inner join that will also display the customer_table as per the serial number in both tables.
SELECT
errorcode_table.error,
errorcode_table.deviceserialnumber,
customer_table.serialnumber,
customer_table.customer,
FROM errorcode_table
INNER JOIN customer_table
ON errorcode_alert_table.deviceserialnumber = customerinfo_table.serialnumber
Now I want to also display the description of the error code as well, here's my attempt:
SELECT
errorcode_table.error,
errorcode_table.serialnumber,
customer_table.serialnumber,
customer_table.customer,
description.serialnumber
description.info
FROM errorcode_table
INNER JOIN customer_table
RIGHT JOIN description_table
ON errorcode_table.deviceserialnumber = customer_table.serialnumber
ON errorcode_table.deviceserialnumber = description_table.serialnumber
Now I'm not getting any records. Please assist.
The ON clause for each join should appear immediately after each join condition. And you can introduce table aliases to make the query easier to read.
SELECT
e.error,
e.serialnumber,
c.serialnumber,
c.customer,
d.serialnumber,
d.info
FROM errorcode_table e
INNER JOIN customer_table c
ON e.deviceserialnumber = c.serialnumber
RIGHT JOIN description_table d
ON e.deviceserialnumber = d.serialnumber;

How to Optimize my JOIN's to speed up my Query?

I have 6 queries like the following query listed below..
each are taking 6 seconds to run
for a total of 36 seconds for page to load
Is there a way to optimize these kinds of queries?
SELECT
tickets.ticketID,
tickets.ticket,
tickets.name1,
tickets.address1,
tickets.city,
tickets.cstate,
tickets.zip,
tickets.caller_type,
tickets.phone,
tickets.caller,
tickets.caller_phone,
tickets.contact,
tickets.contact_phone,
tickets.call_back,
tickets.location,
tickets.printable_text,
tblnotes.ntDate,
tblnotes.ntText,
tblstatus.stDesc,
tblUsers.username
FROM tblusers
RIGHT OUTER JOIN tickets ON tblusers.ID = tickets.ownerID
LEFT OUTER JOIN tblstatus ON tblstatus.stID = tickets.statusID
LEFT OUTER JOIN tblnotes ON tblnotes.ntID = tickets.noteID
WHERE tblstatus.stDesc <> "Closed"
EDIT: try this
SELECT
tickets.ticketID,
tickets.ticket,
tickets.name1,
tickets.address1,
tickets.city,
tickets.cstate,
tickets.zip,
tickets.caller_type,
tickets.phone,
tickets.caller,
tickets.caller_phone,
tickets.contact,
tickets.contact_phone,
tickets.call_back,
tickets.location,
tickets.printable_text,
tblnotes.ntDate,
tblnotes.ntText,
tblstatus.stDesc,
tblUsers.username
FROM tickets
INNER JOIN tblusers ON tblusers.ID = tickets.ownerID
INNER JOIN tblstatus ON tblstatus.stID = tickets.statusID
LEFT OUTER JOIN tblnotes ON tblnotes.ntID = tickets.noteID
WHERE tickets.statusID <> 3
posting as an answer, as I am unable to comment
You have a condition where tblstatus.stDesc <> "Closed"
assuming you have an index here on stID
change that to where tblstatus.stID <> put the id value
also change your left outer joins to inner joins, as any ways you have a where condition, you can keep the left join on tblnotes as I am not sure if it may have a row corresponding to tbltickets
i will also move the tickets table to from and then do an inner join with tblusers
use left outer join only when the join table may not have data, but you still want to show data from your main table

hide duplicate row sql phpmyadmin

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

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 joining Query

I am working on a project and I got stuck into a problem as I have three different tables as shown in the query and snapshots.
I tried to execute the below query and pasting the result of the query
Query
select
m.id,b.paybandname,m.payheadid,h.payheadname,m.basedon,find_in_set(h.id,m.basedon) as
basedonone,m.amount,m.type from tblpay_paybandsystem b
left join tblpay_payheadmapping m on b.id = m.paybandid
left join tblpay_payheads h on m.payheadid = h.id
Result
What I want is based on column I want payheadname for example in third row we have 6 in based on column but I want payheadname which is "Basic" for id=6
Now I am pasting the snap shots of the tables used in the above query
tblpay_paybandsystem
tblpay_payheads
tblpay_payheadmapping
This should do it (not tested):
select
m.id,b.paybandname,m.payheadid,h.payheadname,h2.payheadname,find_in_set(h.id,m.basedon) as
basedonone,m.amount,m.type from tblpay_paybandsystem b
left join tblpay_payheadmapping m on b.id = m.paybandid
left join tblpay_payheads h on m.payheadid = h.id
left join tblpay_payheads h2 on m.basedon = h2.id