Hello i have 2 tables in MySQL DB
status
id, name, date, status
1 server1 datum 0
2 server2 datum 999
3 server3 datum 999
4 server4 datum 0
cis
id, ,url
0 img/offline.png
1 img/server1.png
2 img/server2.png
3 img/server3.png
4 img/server4.png
999 img/online.png
i want to select id and status from status and i want to replace both server_id a and status with url from cis. But i dont know fi this is posible to do or do i need to create a third table for the online/ofline url?
i want to join the tables so that the select returns as this
id, status
img/server1.png, img/offline.png
img/server2.png, img/online.png
img/server3.png, img/online.png
img/server4.png, img/offline.png
According to your comments, you tried the following query:
select s.id,s.name,s.date,s.status,c1.url as serverurl,c2.url as statusurl
from status s left join cis c1 on s.id=c1.id left join cis c2 on s.status=c2.id
This returns the correct resultset, but if I understand what you are looking for, you want this query:
SELECT c1.url AS id, c2.url AS status FROM `status` s
LEFT JOIN `cis` c1 ON s.id = c1.id
LEFT JOIN `cis` c2 ON s.status = c2.id
c1.url is aliased as id and c2.url is aliased as status
Related
I have three tables accounts, receivables and receiveds .
My basic table is receivables I want to get the names from the account table and then subtract the sum of received column in receiveds table form the receivable column in receivables table with same ref numbers. i want to ignore if the balance is zero.
accounts
id
name
22
John
23
Kahn
24
Falis
25
Haseni
26
Gent
receivables
id
receivable
Ref
22
70
A1
24
100
A2
26
60
A3
24
15
A4
receiveds
ref
id
received
A1
22
30
A2
24
60
A1
22
40
A3
26
20
A2
24
10
desired results
id
name
ref
receivable
received
balance
total_id_balance
24
Falis
A2
100
70
30
45
26
Gent
A3
60
20
40
40
24
Falis
A4
15
0
15
45
I have tried this code but it's not working for me
SELECT *
FROM receivables AS rvb
LEFT JOIN accounts AS acc
ON rvb.id = acc.id
LEFT JOIN (SELECT SUM(received) as sum_rvd FROM receiveds) AS rvd
ON acc.id = rvd.id where rvb.receivable>rvd.sum_rvd
INNER JOINs filter out NULL values and the HAVING clause removes zero-balance rows.
SELECT
accounts.* ,
ra.receivable,
rd.received,
ra.receivable - rd.received as balance
FROM accounts
INNER JOIN ( SELECT id, SUM(receivable) as receivable FROM receivables GROUP BY id ) ra
ON ra.id = accounts.id
INNER JOIN ( SELECT id, SUM(received) as received FROM receiveds GROUP BY id ) rd
ON rd.id = accounts.id
HAVING balance > 0
UPDATE
With a ref field we just need to add it as another grouping field for subselects, and change second INNER JOIN to LEFT JOIN (actually looks like it was a mistake from the start, coz we missed entries without receiveds that still had positive balance). Also changing received field to a COALESCE to get zeroes instead of NULL's (indicating non existing rows in a receiveds table).
Since you need an overall per-id total_id_balance field, the natural way to grab it - is using window functions. Note, they are supported only for MySQL 8.0+.
So the resulting query looks like this:
SELECT
accounts.* ,
ra.ref,
ra.receivable,
COALESCE(rd.received, 0) as received,
ra.receivable - COALESCE(rd.received, 0) as balance,
SUM( ra.receivable - COALESCE(rd.received, 0) ) OVER ( PARTITION BY id ) as total_id_balance
FROM accounts
INNER JOIN ( SELECT id, SUM(receivable) as receivable, ref FROM receivables GROUP BY id, ref ) ra
ON ra.id = accounts.id
LEFT JOIN ( SELECT id, SUM(received) as received, ref FROM receiveds GROUP BY id, ref ) rd
ON
rd.id = accounts.id
AND
ra.ref = rd.ref
HAVING balance > 0
ORDER BY ref
This work for me -
EDIT-
Add support for non-unique id in receivables
SELECT acc.id ,acc.name, res.receivable, res.received, (res.receivable - res.received) AS balance
FROM accounts AS acc JOIN
(SELECT recv.id, recv.receivable, rec.received
FROM (
SELECT id, SUM(receivable) AS receivable
FROM receivables
GROUP BY id) AS recv JOIN
(SELECT id, SUM(received) AS received
FROM receiveds
GROUP BY id) AS rec ON rec.id = recv.id
WHERE rec.received < recv.receivable) AS res ON res.id = acc.id;
Assuming it is possible to have an id in receivables without a corresponding id in receiveds, the second join needs to be a LEFT JOIN and you need to handle the NULLs in your SELECT list -
SELECT
a.*,
ra.receivable,
IFNULL(rd.received, 0) received,
ra.receivable - IFNULL(rd.received, 0) balance
FROM accounts a
INNER JOIN (
SELECT id, SUM(receivable) receivable
FROM receivables
GROUP BY id
) ra
ON a.id = ra.id
LEFT JOIN (
SELECT id, SUM(received) received
FROM receiveds
GROUP BY id
) rd
ON a.id = rd.id
HAVING balance > 0;
You can (and should) go a step further and remove the first derived table as it is unnecessary overhead -
SELECT
a.*,
SUM(ra.receivable) receivable,
IFNULL(SUM(rd.received), 0) received,
SUM(ra.receivable) - IFNULL(rd.received, 0) balance
FROM accounts a
INNER JOIN receivables ra
ON a.id = ra.id
LEFT JOIN (
SELECT id, SUM(received) received
FROM receiveds
GROUP BY id
) rd
ON a.id = rd.id
GROUP BY id
HAVING balance > 0;
db<>fiddle
T1 Customers
IDZ NAME MEGAID
123 TOM 32132
124 JEK 32323
125 MAX 32342
126 JIZ 32134
T2 Info:
ID CID GUNS STATUS
1 123 3 1
2 124 4 2
3 126 NULL 1
T3 Status:
ID TYPE
1 Active
2 Inactive
IDZ = CID
I need to return NAME, MEGAID and STATUS (Active/Inactive) for everyone who have NULL on GUNS column from INFO table.
I tried this:
SELECT Customers.Name, CustomersMEGAID, Status.TYPE
FROM Customers
LEFT JOIN Customers ON Info.CID=Custoners.IDZ
WHERE Info.Guns= IS NULL;
But thats doesnt work(
Big thanks if someone can help with this
your question is full with errors but here is a query for you:
SELECT Customers.Name, Customers.MEGAID, Status.TYPE
FROM Customers
LEFT JOIN Info ON Customers.IDZ = Info.CID
INNER JOIN Status ON Info.STATUS = Status.ID
WHERE Info.Guns IS NULL;
You can join all three tables, and then search for nulls in the column.
For example:
select
c.name,
c.megaid,
s.type
from customers c
join info i on i.cid = c.idz
join status s on s.id = i.status
where i.guns is null
So guys, trying to write a query to get the count of statuses where project_id = ? and statuses in 'New' from a couple of tables so let me break it down.
I have these three tables
Case_Status
id case_status
1 New
2 Failed
3. Accepted
Referral
id case_status_id project_id application_id
1 1 1 20
2 2 1 21
Project
id name
1 project1
2 project2
So this is my query
SELECT COUNT(referrals.id) AS count_all, case_statuses.case_status AS counted
FROM "case_statuses" LEFT OUTER JOIN "referrals" ON "referrals"."case_status_id" = "case_statuses"."id"
WHERE "case_statuses"."deleted_at" IS NULL AND (case_statuses.case_status IN ('New') AND referrals.project_id = 1)
GROUP BY case_statuses.case_status;
This is my result
count_all counted
1 New
1 Failed
But I am expecting this result instead
count_all counted
1 New
1 Failed
0 Accepted
Does anyone know what's wrong with my query that isnt showing count for all the case_statuses?
Thanks
Conditions on the second table (in a left join) should be in the on clause:
SELECT COUNT(r.id) AS count_all, cs.case_status AS counted
FROM case_statuses cs LEFT OUTER JOIN
referrals r
ON r.case_status_id = cs.id AND r.project_id = 1
WHERE cs.deleted_at IS NULL AND cs.case_status NOT IN ('New')
GROUP BY cs.case_status;
Otherwise, the WHERE clause turns the outer join into an inner join.
change your query like this
SELECT COUNT(referrals.id) AS count_all, case_statuses.case_status AS counted
FROM "case_statuses" LEFT JOIN "referrals" ON "referrals"."case_status_id" = "case_statuses"."id" AND referrals.project_id = 1
WHERE "case_statuses"."deleted_at" IS NULL AND case_statuses.case_status NOT IN ('New')
GROUP BY case_statuses.case_status;
Given your data and the expected result you just need to loose the WHERE clause.
SELECT COUNT(referrals.id) AS count_all, case_statuses.case_status AS counted
FROM case_statuses
LEFT OUTER JOIN referrals ON referrals.case_status_id = case_statuses.id
GROUP BY case_statuses.case_status;
See this fiddle for details.
In MySQL, I have a two tables as below :
ClientTable
clientID clientName
1 Client A
2 Client B
3 Client C
4 Client D
5 Client E
6 Client F
NotesTable
noteID clientID note noteDate
1 3 Test 1 12-Jun-14
2 3 Test 2 18-Aug-14
3 4 Test 3 23-Oct-14
4 6 Test 4 25-May-14
5 3 Test 5 25-Nov-14
6 6 Test 6 16-Jul-14
I want to select all the clients from the client table and, where a note exists for the client, the date of the latest note entry. If no note exists for a client, then return null for the noteDate. Desired result set as follows :
client ID clientName latestNoteDate
1 Client A null
2 Client B null
3 Client C 25-Nov-14
4 Client D 23-Oct-14
5 Client E null
6 Client F 16-Jul-14
Any help appreciated, I have tried a few options using nested Select with MAX(noteDate) and various left joins but can't seem to get it right.
Why all the subqueries?
select ct.clientID, ct.clientName,max(nt.noteDate) latestNoteDate
from ClientTable ct
left outer join NotesTable nt
on ct.clientID = nt.clientID
group by ct.clientID, ct.clientName
You can use an outer join with a subquery:
select c.clientid, c.clientname, n.latestnotedate
from client c
left join (
select clientId, max(noteDate) latestnotedate
from notes
group by clientId
) n on c.clientId = n.clientId
This assumes the max(noteDate) is the latest note entry. If that's not the case, easy enough to use the noteid instead and then just include one additional join.
Looks like a good place for using a sub-query. Try something like:
select c.id, c.name, n.latestNoteDate from client c
left join
(select clientid, MAX(notedate) as latestNoteDate from note
group by clientid) as n on n.clientid = c.id
The key is, find the data you want from the notes table first, then use that to join with the client data later.
Try the following code, which uses a correlated sub-query.
SELECT ct.clientID,
ct.clientName,
(SELECT MAX(noteDate)
FROM notesTable nt
WHERE nt.clientID = ct.clientId)
FROM clientTable ct
select clienttable.clientID, clienttable.clientName, notes.noteDate
left outer join NotesTable notes on notes.clientID = clienttable.clientID and noteDate = (select max(noteDate) from NotesTable where notes.clientID = clienttable.clientID)
It will return null if there are no note entries.
OR
select clienttable, clientID, clienttable.clientName, (select max(noteDate) from NotesTable where notes.clientID = clienttable.clientID) noteDate
SELECT (select count(u.ag_code)
from table1 as u inner join table2 as tc
on u.industry_id=tc.tempcatid
where u.ag_code!=0) as agnt,
(select count(u.ag_code)
from table1 as u inner join table2 as tc
on u.industry_id=tc.tempcatid where u.ag_code=0),as dircus,
tc.catename from table1 as u inner join table2 as tc
where u.industry_id=tc.tempcatid
group by tc.tempcatid
this query have error
i need two count and category name in one query
this is the condition for count
ag_code!=0
ag_code=0
in table1 have column ag_code (this have 0 and nonzero value)
my result need like this
Full Texts
agent customer catename
11 3 Real Estate
15 1 Automobile
3 0 Medical
34 77 Business
1 45 Travel & Hotels
11 3 Construction & Engineering
SELECT tc.catename,
count(case when u.ag_code!=0 then 1 end) agnt,
count(case when u.ag_code =0 then 1 end) dircus
from table1 as u
inner join table2 as tc on u.industry_id=tc.tempcatid
group by tc.tempcatid, tc.catename
Hi There you might be able to use the below example to get back the result you require.
Select SUM(Inactive) Inactive ,SUM(Active) Active FROM
(
Select Count(t1.UserId) Inactive,0 Active
FROM
(select * from users where inactive=1) as t1
UNION
SELECT 0 Inactive,Count(t2.UserId) Active FROM
(select * from users where inactive=0) as t2
) as result