Here is query:
SELECT
t1.id, t1.data, t1.invoice_nr, t3.tiekejas, t4.uzsakovas, t1.pard_suma, t1.pirk_suma,
t1.transporto_suma, t2.apm_id, t2.apm_data, t2.apm_suma, t2.tipas, t1.terminas,
t5.regionas, t6.imone, t1.masina, t7.vardas, t7.pavarde, t8.vardas, t8.pavarde
FROM
irasai t1
LEFT JOIN apmokejimai t2 ON t1.invoice_nr = t2.apm_invoice_nr
LEFT JOIN tiekejai t3 ON t1.tiekejas = t3.id
LEFT JOIN uzsakovai t4 ON t1.pirkejas = t4.id
LEFT JOIN regionai t5 ON t1.regionas = t5.id
LEFT JOIN transportas t6 ON t1.transporto_imone = t6.id
LEFT JOIN vadybininkai t7 ON t1.pard_vad = t7.id
LEFT JOIN vadybininkai t8 ON t1.pirk_vad = t8.id
GROUP BY t1.invoice_nr, t2.apm_id
ORDER BY t1.data, t1.invoice_nr
And the problem is that table t1 some columns(tiekejas, pirkejas, regionas, trasporto_imone, pard_vad, pirk_vad) is sets of ids(ex. 1,2,5 etc). This query returns value only of one id from set, I want to get it all in one row (every value in new line or separated by dot or something, I can split them later in program). For now I split them in program, but I was required to select them one by one from database, and that takes too much time, and programs working too slow then.
I hope there is a better solution for this. Thanks.
HERE IS SQL FIDDLE LINK: http://sqlfiddle.com/#!9/d8609/1
EDIT:
http://postimg.org/image/3sostf42n/
This what I need to get, defined column is t1.tiekejas (ex. 1,2,5), this ids have values in table t3 (tiekejai), by this query it only returns one value by id 1, but I need three values of ids 1,2,5 returned.
Your initial problem can be fixed using FIND_IN_SET:-
SELECT
t1.id, t1.data, t1.invoice_nr, t3.tiekejas, t4.uzsakovas, t1.pard_suma, t1.pirk_suma,
t1.transporto_suma, t2.apm_id, t2.apm_data, t2.apm_suma, t2.tipas, t1.terminas,
t5.regionas, t6.imone, t1.masina, t7.vardas, t7.pavarde, t8.vardas, t8.pavarde
FROM
irasai t1
LEFT JOIN apmokejimai t2 ON t1.invoice_nr = t2.apm_invoice_nr
LEFT JOIN tiekejai t3 ON FIND_IN_SET(t3.id, t1.tiekejas)
LEFT JOIN uzsakovai t4 ON FIND_IN_SET(t4.id, t1.pirkejas)
LEFT JOIN regionai t5 ON FIND_IN_SET(t5.id, t1.regionas)
LEFT JOIN transportas t6 ON FIND_IN_SET(t6.id, t1.transporto_imone)
LEFT JOIN vadybininkai t7 ON FIND_IN_SET(t7.id, t1.pard_vad)
LEFT JOIN vadybininkai t8 ON FIND_IN_SET(t8.id, t1.pirk_vad)
GROUP BY t1.invoice_nr, t2.apm_id
ORDER BY t1.data, t1.invoice_nr
However I think you are going to have issues with the multiple values from each joined table. Further you are using GROUP BY to eliminate the multiple rows, and which one of the various values for each column for a particular invoice_nr / apm_id that is chosen is not defined.
Note, also it is generally a bad idea to have a field containing a comma separated list of ids. It makes doing a join slow and harder than it should be (as indexes cannot effectively be used on the field), and it limits future increases in the data. It is generally a sign of a poorly normalised database design.
Related
How to implement WHERE conditions in LEFT_JOIN. My query is:
SELECT t1.company_short_name, COUNT(t2.user_ip_address) as chart_accessed
FROM tblcompanymaster t1
LEFT JOIN tblhistorymaster t2 ON t1.row_id = t2.company_id
WHERE t2.user_last_request_time BETWEEN 1603775329 AND 1606280929
GROUP BY t1.company_short_name
Where I need to found all the company_short_name with the count of access, and if there is no access or the count is 0 then it should come COUNT(t2.user_ip_address) is 0. When I am not using where condition its working perfectly but after the use of where condition its giving only the result where the count is greater then 1. I have tried a lot but I am not able to modify the code. Any suggestions will be of great help
Put your where clause condition to ON clause
SELECT t1.company_short_name, COUNT(t2.user_ip_address) as chart_accessed
FROM tblcompanymaster t1
LEFT JOIN tblhistorymaster t2 ON t1.row_id = t2.company_id
and t2.user_last_request_time BETWEEN 1603775329 AND 1606280929
GROUP BY t1.company_short_name
You can add the filtering (where) component to the join instead - only taking the values if they exist in t2.
SELECT t1.company_short_name, COUNT(t2.user_ip_address) as chart_accessed
FROM tblcompanymaster t1
LEFT JOIN tblhistorymaster t2 ON t1.row_id = t2.company_id AND t2.user_last_request_time BETWEEN 1603775329 AND 1606280929
GROUP BY t1.company_short_name
I am hoping someone can point me in the correct direction, I am just an amateur playing with a hobby.
I have 2 tables, first table 'tbl_wheel_diameter' has just 'wheel_diameter' and 'id'
Second table 'tbl_tyres' has 'wheel_diameter_front','wheel_diameter_rear' and 'vehicleId'
I need to write a query that selects the front and rear diameter together.
This is something I tried.
SELECT tbl_wheel_diameter.wheel_diameter_front,tbl_wheel_diameter.wheel_diameter_rear
FROM tbl_tyres
INNER JOIN tbl_wheel_diameter
ON tbl_wheel_diameter.id = tbl_tyres.wheel_diameter
WHERE tbl_tyres.vehicleId = 2
I have read that I should use left joins but I seem to get confused with how to write them?
Add an alias for the table so you can include the same table twice. Here is an example using an alias of t1 and t2.
SELECT t1.wheel_diameter,t2.wheel_diameter
FROM tbl_tyres
INNER JOIN tbl_wheel_diameter t1
ON t1.id = tbl_tyres.wheel_diameter_front
INNER JOIN tbl_wheel_diameter t2
ON t2.id = tbl_tyres.wheel_diameter_rear
WHERE tbl_tyres.vehicleId = 1
My tables:
T1 = Projects
T2 = bid_details
T3 = bid_submission_details
T4 = quote_submission_details
This is how Data flow Works:
SQL Code:
"SELECT *,T3.bidsubmit_date,T4.Quotesubmit_date FROM `Projects` T1
LEFT JOIN `bid_details` T2 ON(T2.proj_reff = T1.proj_reff)
LEFT JOIN `bid_submission_details` T3 ON(T3.bid_reff = T2.bid_reff)
LEFT JOIN `quote_submission_details` T4 ON(T4.proj_reff = T1.proj_reff)
ORDER BY T3.bidsubmit_date,T4.Quotesubmit_date ASC";
Result View:
I have tried many ways to ORDER BY the submission date. but nothing worked. In the result view blue highlighted row is 2014 dated but it not going at the top of the list when I Order by Ascending order.
It always Orders the result from Quote Submission table first and then Order the result from Bid Submission table. Project types can be seen the in the result view.
i want a way to get Order by submission date no matter what type of project it is!
using COALESCE() function to the filed from the other three tables together.
SELECT *,T3.bidsubmit_date,T4.sub_date,
COALESCE(T3.bidsubmit_date,T4.sub_date) AS SubmiDates
FROM `$SQLTable` T1
LEFT JOIN `quote_submission_details` T4 ON(T4.pro_ref_no = T1.ref_no)
LEFT JOIN `bid_details` T2 ON(T2.pro_ref_no = T1.ref_no)
LEFT JOIN `bid_submission_details` T3 ON(T3.bid_ref_no = T2.bid_ref_no)
ORDER BY SubmiDates ASC";
after many very much goggling found a solution !!!
I've got a query as follows:
SELECT COUNT(Table1.Identifier) AS NonCancelCnt
FROM Table1, Table2
LEFT JOIN eventattendees ON eventattendees.AttendeeID = 47322
LEFT JOIN eventsignup ON eventattendees.AttendeeID = eventsignup.AttendeeID
LEFT JOIN transactions on transactions.registrationID=eventsignup.regid
WHERE ((eventsignup.EventID = Table1.Identifier) Or (eventsignup.EventID = Table1.AttendanceLinkID))
The "OR" clause is causing no index to be used. If I remove either portion, my execution path goes from 95,000 to 200, and speed is drastically increased.
I'm not very experienced in reworking such a thing, what is my best option for doing so?
First, you should rewrite your query to specify how Table1, Table2 and eventattendees are joined. Also choose whether you want to specify the columns to use to join in the WHERE clause or after the JOIN keyword. After you clean it up a bit, the optimizer may do a better job of picking the proper index to use.
If that still doesn't work, you can use a SQL hint to specify the index you want the optimizer to use:
WITH INDEX(IX_nameofindex)
SELECT COUNT(Table1.Identifier) AS NonCancelCnt
FROM Table1, Table2
LEFT JOIN eventattendees ON eventattendees.AttendeeID = 47322
LEFT JOIN eventsignup ON eventattendees.AttendeeID = eventsignup.AttendeeID
LEFT JOIN transactions on transactions.registrationID=eventsignup.regid
WHERE eventsignup.EventID = Table1.AttendanceLinkID
union all
SELECT COUNT(Table1.Identifier) AS NonCancelCnt
FROM Table1, Table2
LEFT JOIN eventattendees ON eventattendees.AttendeeID = 47322
LEFT JOIN eventsignup ON eventattendees.AttendeeID = eventsignup.AttendeeID
LEFT JOIN transactions on transactions.registrationID=eventsignup.regid
WHERE eventsignup.EventID = Table1.Identifier
Not understanding what Table1 and Table2 are, nor are they joined in any shape, you will get a Cartesian result (for each record in Table1, will be joined with each record in Table2)
Additionally, your where clause could just be simplified with an IN clause
where
eventsignup.EventID IN ( Table1.Identifier, Table1.AttendanceLinkID )
After 5.0.12 MySQL changed the syntax for left joins to match SQL2003 standard. So
... FROM t1 , t2 LEFT JOIN t3 ON (expr)
needs to be rewritten as
... FROM (t1 , t2) LEFT JOIN t3 ON (expr
or else it will be parsed as ... FROM t1 , (t2 LEFT JOIN t3 ON (expr))
Now, I have an ancient app I'm porting from MySQL 3.23 (eek!) to 5.1, and the old code has this query:
select b.*, c.*, g.*, p.perfname, p.persname
from bookings b, customer c
left join grade g on b.chrggrade=g.grcode
left join person p on b.person=p.percode
where complete='Y' and invoiced='N'
and datemade between '2009-03-25' and '2009-03-31'
and c.custcode=b.cust
order by cust, person, tsref, stdt
This fails with SQL error 1054, unknown column in b.chrggrade. This is because it's parsing as
select b., c., g.*, p.perfname, p.persname
from bookings b, (customer c
left join grade g on b.chrggrade=g.grcode )
left join person p on b.person=p.percode
where complete='Y' and invoiced='N'
and datemade between '2009-03-25' and '2009-03-31'
and c.custcode=b.cust
order by cust, person, tsref, stdt
I think.
I'm sure correctly placed brackets can fix this but I'm stumped.
I found reference to this change at http://bugs.mysql.com/bug.php?id=13551, which shows how to fix a simple left join, but I still can't work it out for this query.
David
Stop using the comma syntax altogether and be explicit in your JOIN statements. The comma syntax forces you to put the JOIN condition in the WHERE clause, which may not get executed until after LEFT/RIGHT joins, depending on how it's parsed. Using explicit JOINS makes the query more readable anyway.
...FROM t1, t2 LEFT JOIN t3 ON (expr)
becomes
...FROM t1 INNER JOIN t2 ON (expr) LEFT JOIN t3 ON (expr)
That will also fix the error you are seeing. Unless there is no chrggrade in the bookings table, then nothing will fix the error.