I have two tables as follows:
tableA
pat_id name user_id
1 sam 1
2 jose 1
3 sandra 2
tableB
id pat_id pat-main_name
1 1 MR0001
2 3 MR0005
I am expecting the result as follows:
pat_id name user_id pat-main_name
1 sam 1 MR0001
2 jose 1
I have user_id = 1
What I did is as follows:
SELECT tableA.pat_id,tableA.name,tableA.user_id,tableB.pat-main_name
FROM tableA
LEFT OUTER JOIN tableB
where tableA.pat_id = tableB.pat_id AND tableA.user_id;
and I am getting result as:
pat_id name user_id pat-main_name
1 sam 1 MR0001
2 jose 1
3 Sandra 2
What should I do?
This query properly joins the 2 tables:
SELECT tableA.pat_id
,tableA.name
,tableA.user_id
,tableB.`pat-main_name`
FROM tableA
LEFT OUTER JOIN tableB
ON tableA.pat_id = tableB.pat_id;
Your original query was sort of mixing the old syntax and the newer ANSI syntax which should be preferred:
old syntax = FROM a, b WHERE a.x = b.x...
new ANSI = ... JOIN ON a.x = b.x...
You can restrict the output by adding a WHERE clause such as WHERE tableA.user_id = 1 .
It will give this output:
1 sam 1 MR0001
2 jose 1
3 Sandra 2 NULL
Or with a WHERE clause tableA.user_id = 1:
1 sam 1 MR0001
2 jose 1
Use left join
SELECT tableA.pat_id,tableA.name,tableA.user_id,tableB.pat-main_name
FROM tableB left join tableA
ON tableB.pat_id = tableA.pat_id
WHERE tableA.user_id=1
This will work:
SELECT
tableA.pat_id, tableA.name, tableA.user_id,
tableB.`pat-main_name`
FROM tableA LEFT OUTER JOIN tableB ON tablea.pat_id = tableb.pat_id
Where tablea.user_id = 1
you can write query as follow:
SELECT `t`.`name`,
`t1`.`pat_id`,
`t1`.`pat-main_name`
FROM tableA AS t LEFT JOIN tableB AS t1 ON `t`.`pat_id` = `t1`.`pat_id`
Related
I have four table and structure as below :
1)Budget
id Budget_name
1 test1
2 test2
3 test3
2)Yearly Budget
id amount_yearly budgetid
1 1000 1
2 2000 2
3 5000 3
ri_spent
id Spent_amount budgetid
1 100 2
2 100 2
3 200 3
4)FI_spent
id Spent_amount budgetid
1 100 2
2 100 3
3 200 3
i want to fetch data accourding to or based on first budget table id
below is the query i was trying:
select d.centers as Cost_Center,
ud.BUDGET_ANNUAL_AMOUNT as Annual_Budget,
l.LEAD_ID as Lead_Id,
l.AMOUNT as Lead_Amount,
f.FINANCEADD_ID as Finance_Id,
f.AMOUNT as Finance_Amount
from Cost_centers as d
inner join ANNUAL_BUDGET_BUDGET_CENTER as ud on d.id = ud.BUDGET_ID
inner join RI_DETAILS as l on l.COST_CENTER = d.id
inner join F_RI_DETAILS as f on f.COST_CENTER = d.id
ORDER BY d.id DESC
I want output of the following way:
Id Name ri_id FI_ID RI_Spent_Amount FI_Spent_Amount Annual_Buget
1 test1 1000
2 test2 1 1 100 200 1000
2 test2 2 100 2000
3 test3 3 2 300 100 2000
3 test3 3 200 2000
Any way if possible then please help me.
I want to minus annual budget with spent_amount later.
If possible then help me .
Please Try This Query ,
select b.id ,
b.Budget_name,
y.id,
y.amount_yearly,
r.id,
r.ri_spent,
f.id,
f.Spent_amount
from Budget b
INNER JOIN Yearly_Budget y on y.budgetid =b.id
INNER JOIN ri_spent r on r.budgetid =b.id
INNER JOIN FI_spent f on f.budgetid =b.id
ORDER BY b.id DESC
Could you please use left join instead of inner join. Requirement and sql arent matching so i may be little off. But you can get the idea.
select d.centers as Cost_Center,
ud.BUDGET_ANNUAL_AMOUNT as Annual_Budget,
l.LEAD_ID as Lead_Id,
l.AMOUNT as Lead_Amount,
f.FINANCEADD_ID as Finance_Id,
f.AMOUNT as Finance_Amount
from Cost_centers as d -- I assume this is the budget table
left outer join ANNUAL_BUDGET_BUDGET_CENTER as ud on d.id = ud.BUDGET_ID
left outer join RI_DETAILS as l on l.COST_CENTER = d.id
left outer join
(select sum(FINANCEADD_ID) FINANCEADD_ID,sum(AMOUNT) AMOUNT,id from
F_RI_DETAILS group by id) as f
on f.COST_CENTER = l.id
ORDER BY d.id DESC
I have two SQL-tables like this:
T1
Animal Name
Cat Paul
Cat Miau
Cat Paul
Cat Peter
T2
Legs Name
4 Paul
4 Miau
3 Paul
4 Peter
What I want to have is a table like this:
Animal Legs Name
Cat 4 Miau
Cat 4 Peter
I want to have all animals with a specific number of legs but not when we have two cats with the same name.
I tried doing something like these:
select a.animal, b.legs, a.name
from animallistA as a join animallistB as b
on a.name = b.name
where b.legs = 4 and not b.legs = 3
group by a.animal, b.legs, a.name
If I say where b.legs = 4 then I'll also receive 'Paul' but If I say where b.legs = 4 and not b.legs = 3 I receive nothing at all.
Is there a way not to receive the cats who have the same name and 4 legs but not the one that have the same name but either 4 or less(or more) legs.
NOT EXISTS may do the job.
SELECT t1.animal, t2.legs, t2.name
FROM t1
JOIN t2 ON t1.name = t2.name
WHERE t2.legs = 4 AND
NOT EXISTS
(
SELECT 1 FROM t2 WHERE t2.name = t1.name AND t2.legs != 4
)
You can first take all the unique names of 4 legs animals ( from subquery ) and then join with the main table to get required result.
select a.animal, b.legs, a.name from animallistA as a join
(select name,max(legs) legs from animallistB group by name having count(distinct legs)=1) as b
on a.name = b.name and b.legs=4
From subquery we can get result
Legs Name
4 Miau
4 Peter
Because distinct values in having condition fail for Paul. Joining this with main table gives the requested result
AS far as I understood your question I suggest following query:
SELECT A.ANIMAL, A.NAME, B.LEGS
FROM ANIMALLISTA A
INNER JOIN ANIMALLISTB B ON A.NAME = B.NAME
LEFT JOIN (SELECT NAME, COUNT(*) AS RC FROM ANIMALLISTB GROUP BY NAME) C ON A.NAME = C.NAME
WHERE B.LEGS=4
AND C.RC=1
Output:
ANIMAL NAME LEGS
Cat Miau 4
Cat Peter 4
You might wanna try something like this, worked for me on My SQL 8.0.2
|| cat_1 = T1 and cat_2 = T2
WITH CTE AS
(
SELECT distinct *
FROM cat_1
INNER JOIN cat_2 ON cat_1.names=cat_2.name
),
SCTE AS
(
SELECT CTE.animal,
CTE.legs,
CTE.names,
COUNT(CTE.names) over(PARTITION BY CTE.names) as Count
from CTE
)
SELECT
SCTE.animal,
SCTE.legs,
SCTE.names
FROM SCTE
WHERE legs = 4
AND Count = 1;
Output:
animal legs names
Cat 4 Miau
Cat 4 Peter
I am trying to get a count(*) for different column from a different table using union.
//tbl_churidar
order_id order_no_first order_no
--------------------------------------
1 C 1000
2 C 1001
3 C 1002
//tbl_anarkali
order_id order_no_first order_no
--------------------------------------
1 A 1003
2 A 1004
3 A 1005
//tbl_assign
assign_id order_id order_no_first
---------------------------------------
1 1 C
2 1 A
3 2 C
4 3 C
5 2 A
6 3 A
//tbl_unit_status
status_id assign_id status_status stitching_worker
-----------------------------------------------------------
1 1 Stitch AA
2 2 QC {null}
3 3 Stitch BB
4 4 Stitch BB
5 5 Stitch AA
6 6 Stitch CC
from the table tbl_unit_status where status_status = Stitch should INNER JOIN with other two table and get the total count of churidar and anarkali each stitching_worker taken.
the required output is,
churidar anarkali stitching_worker
----------------------------------------
1 1 AA
2 0 BB
0 1 CC
I have tried to get the above output but got stuck. Below is my code,
SELECT churidar, anarkali, stitching_worker
FROM ((
SELECT count(*) AS churidar, NULL AS anarkali,
us.stitching_worker
FROM tbl_unit_status us
INNER JOIN tbl_assign a ON a.assign_id = us.assign_id
INNER JOIN tbl_churidar o ON
(o.order_id = a.order_id AND
o.order_no_first = a.order_no_first)
INNER JOIN tbl_contacts c ON c.contacts_id = o.contacts_id
LEFT JOIN tbl_title t ON t.title_id = c.title_id
WHERE us.status_status = "Stitch" AND
o.order_no_first = "C"
GROUP BY us.stitching_worker
)
UNION (
SELECT NULL AS churidar, count(*) AS anarkali,
us.stitching_worker
FROM tbl_unit_status us
INNER JOIN tbl_assign a ON a.assign_id = us.assign_id
INNER JOIN tbl_anarkali o ON (
o.order_id = a.order_id AND
o.order_no_first = a.order_no_first)
INNER JOIN tbl_contacts c ON c.contacts_id = o.contacts_id
LEFT JOIN tbl_title t ON t.title_id = c.title_id
WHERE us.status_status = "Stitch" AND
o.order_no_first = "A"
GROUP BY us.stitching_worker
)
) AS T1
the output for the above code is,
churidar anarkali stitching_worker
----------------------------------------
1 0 AA
{null} 1 AA
2 0 BB
0 1 CC
how to get the required output. I have tried a lot. Help me find the answer. Thankyou.
If I understand correctly (which I may not), you don't need the first two tables. You can get the information you need from tbl_assign and just use aggregation:
select us.stitching_working,
sum(a.order_no_first = 'C') as churidar,
sum(a.order_no_first = 'A') as anarkali
from tbl_unit_status us join
tbl_assign a
on us.assign_id = a.assign_id
where us.status_status = 'Stitch'
group by us.stitching_working;
I have been trying to solve this issue for a while, hope anyone help me. I am having two table, the first table is
Table Name : OnlineTest
OnlineTestId category subcategory
1 English Spelling
2 English Grammar
3 English Antonyms
4 English Synonyms
The second table is
Table Name : UserStatus
Id userId status onlineTestId
1 1 Finished 1
2 1 Not Finished 2
3 2 Not Finished 1
4 2 Finished 3
5 3 Not Finished 4
Result
OnlineTestId userId status
1 1 Finished
2 1 Not Finished
3 null null
4 null null
I have tried this query,
select c.onlinetestid, d.userid, d.status from onlinetest c left join userstatus d on d.onlinetestid = c.onlinetestid
where c.category = 'English' and d.userid = 1;
But this query is bring the first two row of the result and not the last two, in which the userId and status are null.
How to bring the above result?
Place the d.userid = 1 predicate in the ON clause:
select c.onlinetestid, d.userid, d.status
from onlinetest c
left join userstatus d on d.onlinetestid = c.onlinetestid and d.userid = 1
where c.category = 'English'
This will return all rows from onlinetest, having columns of userstatus filled with nulls where predicate d.userid = 1 fails.
You can also use left outer Join as below :
SELECT c.OnlineTestId, d.userId, d.status
FROM OnlineTest AS c LEFT OUTER JOIN
UserStatus AS d ON d.onlineTestId = c.OnlineTestId AND d.userId = 1
WHERE (c.category = 'English')
I am having a problem with MySQL joins.
Table_A:
A_id Cost1 A1_id Cost2
1 500 0 200
1 100 1 100
1 50 2 60
1 10 3 50
2 5 0 10
Table_B (Refers B_id: from Table_A A1_id):
B_id FName LName
1 X A
2 Y B
3 Z C
Table_C (Refers C_id: from Table_A A_id):
C_id Towns
1 Atlanta
2 NewYork
I need to combine all three tables, like the following output:
I extract the Towns that match (Table_A.A_id=Table_C.C_id).
I extract the Fname,Lname that match (table_A.A1_id=Table_b.b_id).
I need to skip the Towns if A1_id != 0.
I need to skip the Fname,Lname if A1_id == 0.
The remaining data may either be a value or null, which I specify as '#'.
What would be an efficient MySQL query for the given scenario?
Output:
A_id Cost1 A1_id cost2 Fname Lname Towns
1 500 0 200 # # Atlanta
1 100 1 100 X A #
1 50 2 60 Y B #
1 10 3 50 Z C #
I think it should be something like this.
select A_id, Cost1, A1_id, cost2, Fname, Lname, Towns
from Table_A
left join Table_B on table_A.A1_id = Table_b.b_id
left join Table_C on Table_A.A_id = Table_C.C_id
This looks like a UNION of two distinct queries to me. I'm going to assume that the ID columns never contain negative values.
SELECT A.A_id, A.Cost1, A.A1_id, A.Cost2, B.Fname, B.Lname, C.Town
FROM Table_A AS A
INNER JOIN Table_B AS B ON A.A1_id = B.B_id
LEFT OUTER JOIN Table_C AS C ON A.A_id = C.C_id
WHERE A.A1_id != 0
AND C.C_id < 0
UNION
SELECT A.A_id, A.Cost1, A.A1_id, A.Cost2, B.Fname, B.Lname, C.Town
FROM Table_A AS A
LEFT OUTER JOIN Table_B AS B ON A.A1_id = B.B_id
INNER JOIN Table_C AS C ON A.A_id = C.C_id
WHERE A.A1_id = 0
AND B.B_id < 0;
I'm not completely confident about that formulation. There's a chance that the conditions on B.B_id < 0 and C.C_id < 0 need to be associated with the corresponding ON clauses.
There's also a decent chance that using two left outer joins in a single SELECT with appropriate OR'd filters would achieve the correct result.
If I understand correctly, it should be:
select fname, lname, towns
from table_a, table_b table_c
where table_a.a_id = table_c.c_id
and table_a.a1_id = table_b.b_id
and table_a.a1_id <> 0;