how to fetch data from multiple table? [closed] - mysql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
what i want is all the record from tbmedAssign where zoneid or zone name given as parameter.
how to wrote query for this?
please help.
table name
tablzone
zone_id(PK) ZoneName
----------- --------
1 east
2 west
3 north
4 south
tbluser
usrId(PK) userzoneId(FK to tblzone) username
-------- ------------------------- ------------
1 1 manish
2 3 rahul
3 2 ankit
4 4 amir
5 2 rashmi
6 1 akash
tbldoctor
docId(PK) usrId(Fk to tbluser) docname
-------- -------------------- ------------
1 2 hemant
2 2 chintu
3 3 rahim
4 1 salman
5 3 kishor
6 3 saurabh
7 2 banti
tblmedAssign
transId(Pk) doctorId(FK to tbldoctor) medId(FK) dateInsert
---------- ------------------------- ------ -----------
1 2 2 20/12/2012
2 3 3 21/12/2012
3 2 3 23/12/2012
4 4 1 24/12/2012
tblmedia
medid(PK) medianame
--------- ---------
1 casfung
2 inem
3 media1
4 tplan
5 casfung test
i want all the record from tblmedAssign where doctor belongs to particular user in tbluser and in the tbluser user is belongs to particular zone and zone id is provided as parameter? for example zoneid= 1;
i want to select medianame too in the record

Basically you need to join the four tables with their linking columns. Try this,
SELECT a.*,
b.*,
c.*,
d.*,
e.*
FROM tblmedAssign a
INNER JOIN tblDoctor b
ON a.doctorID = b.docID
INNER JOIN tblUser c
ON b.usrID = c.usrID
INNER JOIN tblZone d
ON c.userzoneID = d.zone_ID
INNER JOIN tblmedAssign e
ON e.medid = a.medid
WHERE d.zone_id = #zone_id OR -- supply value here
d.zoneName = #zoneName

By JOINing the three tables, something like:
SELECT -- what you want to select
FROM tblmedAssign ta
LEFT JOIN tbldoctor td ON ta.doctorId = td.docId
LEFT JOIN tbluser tu ON td.usrId = tu.usrId
LEFT JOIN tablzone tz ON tu.userzoneId = tz.zone_Id
WHERE tz.zone_Id = #zoneIdParam

select *
from tblmedAssign
inner join tbldoctor on tbldoctor.docId = tblmedAssign.doctorId
inner join tbluser on tbluser.usrId = tbldoctor.usrId
inner join tablzone on tablzone.zone_id = tbluser.userzoneId
where tablzone.ZoneName = 'east'
or tablzone.zone_id = 2

This should serve your purpose
select *
from
tblmedAssign, tbldoctor, tbluser, tablzone
where
tblmedAssign.doctorId = tbldoctor.docId and
tbldoctor.usrId = tbluser.usrId and
tbluser.userzoneId = tablzone.zone_id and
(tablzone.zone_id = x or tablzone.ZoneName = 'y')

Related

Sorting mysql row data to get only a specific id [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a table as shown below
id | t_id | u_id
---+------+------
1 | 2 | 2
2 | 1 | 2
3 | 1 | 1
4 | 2 | 2
5 | 1 | 1
I am trying to get all t_id with u_id of 2 but once without the t_id ever having a u_id of 1 in the history of the whole table.
I tried
SELECT
C_Name, count(*) as count
FROM tenter
WHERE C_Date = '20200127' AND L_TID = '2';
But this gives me the record of all L_TID = 2 and does not filter out those with previous record of L_tid = 1.
Expected result: get all U_ID without the previous history of L_TID = 1, it should get only those without ever having L_tid =1.
Thanks in advance.
One method is aggregation and a having clause:
select t_id
from tenter t
group by t_id
having sum(u_id = 2) > 0 and -- has "2"
sum(u_id = 1) = 0; -- does not have "1"
If you have another table of t values, then exists/not exists might be more efficient:
select t.t_id
from t
where exists (select 1 from tenter tt where tt.t_id = t.t_id and tt.u_id = 2) and
not exists (select 1 from tenter tt where tt.t_id = t.t_id and tt.u_id = 1);
I think you want not exists:
select t.*
from tenter t
where
u_id = 2
and not exists (
select 1 from tenter t1 where t1.t_id = t.t_id and t1.u_id = 1
)
You can also use aggregation, if you just want a list of t_ids. If 1 and 2 are the only possible values, you can just do:
select t_id
from tenter t
group by t_id
having min(u_id) = 2
If there are other possible values:
having max(u_id = 1) = 0 and max(u_id = 2) = 1

MYSQL: Left join same table twice with same field [duplicate]

This question already has answers here:
MYSQL: How to JOIN two tables on the same query referencing the same table twice
(3 answers)
Closed 4 years ago.
I have a users table that stores admin and normal customers:
ID Name Role
1 John Admin
2 Sara User
3 Pete User
4 Nick User
Another Table that stores requests:
ID Updatedby Createdby DateCreated DateUpdated
1 1 2 10/12/2017 21/01/2018
2 1 1 11/12/2017 22/01/2018
3 1 3 12/12/2017 23/01/2018
4 2 2 13/12/2017 24/01/2018
I am trying to create a view that looks like this:
ID Updatedby Createdby DateCreated DateUpdated
1 John Sara 10/12/2017 21/01/2018
2 John John 11/12/2017 22/01/2018
3 John Pete 12/12/2017 23/01/2018
4 Sara Sara 13/12/2017 24/01/2018
The problem I am facing when creating the query is, I am trying to left join to the user table twice. How do I specify whether the Users.Name is for Updatedby or Createdby? This is what I mean:
SELECT
request.ID,
users.Name, /*name for Createdby */
users.Name, /*name for Updatedby */
requests.DateCreated,
requests.DateUpdated
FROM
requests
LEFT JOIN
users ON requests.Updatedby = users.ID
I can't get my head around the logic with this.
this is an example:
SELECT toD.dom_url AS ToURL,
fromD.dom_url AS FromUrl,
rvw.*
FROM reviews AS rvw
LEFT JOIN domain AS toD
ON toD.Dom_ID = rvw.rev_dom_for
LEFT JOIN domain AS fromD
ON fromD.Dom_ID = rvw.rev_dom_from
you can use Alias

I want to fetch data from 2 different table

I want to fetch result master.category wise suppose if i have
$category ='doctor'
then how can i get the result? i have drawn the tables below and expected result also, Please help me. thanks
table name->Master
------------------
id label category
1 expertise doctor
2 fee doctor
3 appontment doctor
4 services lawyer
5 qualification student
table name->Field
------------------
id label_id Information
1 1 desntist
2 1 general_physician
3 1 general_surgeons
4 4 criminal_law
5 5 civil_law
expected result
--------------------
expertise
dentist
general_physician
general_surgeons
Perform a JOIN like
select f.information as 'expertise'
from field f
join master m on m.id = f.label_id
where m.category = 'doctor';
its query give all information by master's id
SELECT m.label, GROUP_CONCAT(Information)
FROM Master m
JOIN Field f
ON m.id = f.label_id
WHERE m.category = 'doctor'

MySQL JOIN query -

I know this question has already asked here, but I can't figure this one out from the previous answers.
We've got 2 tables:
members
-----------------------
| id | country_iso_3 |
-----------------------
1 USA
2 DZA
3 FRA
4 ILI
5 USA
6 USA
members_details
-----------------------
| member_id | city |
-----------------------
1 AA
2 BB
3 CC
4 DD
5 EE
6 FF
Now I want to query members_details and select the cities which are from the same countries, here "AA", "EE" and "FF" should be the results (because the members are from USA)
I know how to compare different cols from different tables, but here we need to check the second table 'member_id' and the first table 'id (country_iso_3)' somehow!
Select city from members LEFT JOIN members_details
ON members.id = members_details.memberid Where country_iso_3 = 'USA'
Just JOIN the two tables, with a WHERE clause for the country you want to get the city of it like so:
SELECT md.city
FROM members m
INNER JOIN members_details md ON m.id = md.memberid
WHERE m.country_iso_3 = 'USA'
SQL Fiddle Demo
This should work, and not only for USA, but for any country which is listed more than once:
SELECT d.city
FROM members m,
members_details d
WHERE d.member_id = m.id
AND m.country_iso_3 IN (
SELECT country_iso_3
FROM members
GROUP BY 1
HAVING count(1) > 1
)

Conditional Table change in MySQL Query

I tried to work out around this. But I think I am getting no where.
I have 3 tables:
This tale contains all questions and question types:
Table: Ref
id | type | info
==========================
1 SS Education
---------------------------
2 RB Gender
---------------------------
3 ST State
This table contains "options" for the questions in the above table 'Ref`
Table: ref_ans
id | q_id | answer_text
===========================
1 1 Masters
---------------------------
2 1 Bachelors
---------------------------
3 1 Undergrad
---------------------------
4 2 Male
---------------------------
5 2 Female
---------------------------
6 2 Dont want to disclose
This table contains states (type ="ST" in table Ref)
Table: us_states
id | answer_text
===========================
1 Alaska
---------------------------
2 Alabama
---------------------------
3 Arkansan
---------------------------
4 Arizona
---------------------------
5 Baltimore
---------------------------
etc
The result I want is:
ref.id, ref_ans.id, ref.answer_text / us_states.answer_text
*for a given ref.question_id *.
And the condition is: If the question_id, for which the answers requested is 'ST', it should pull the answers from us_states, otherwise, it should pull from ref-ans table.
I tried this. Obviously, this did not work:
SELECT ref.id,
CASE WHEN ref.type = 'ST' THEN
(SELECT ID, answer_text FROM us_states )
ELSE
(SELECT id, answer_text FROM ref_ans)
END
FROM ref
WHERE ref.ID = <id>
Any ideas?
Try:
SELECT a.id,
COALESCE(b.id, c.id) AS ans_id,
COALESCE(b.answer_text, c.answer_text) AS answer_text
FROM ref a
LEFT JOIN ref_ans b ON a.id = b.q_id
LEFT JOIN us_states c ON a.type = 'ST'
WHERE a.id = <id> AND (
(a.type <> 'ST' AND b.id IS NOT NULL) OR
(a.type = 'ST' AND c.id IS NOT NULL)
)
Assuming all the three tables are connected by id(1st column):
SELECT ref_ans.id, ref.id, if(ref.type='ST', us_states.answer_text, ref_ans.answer_text) as answer_text
FROM ref_ans
JOIN ref on ref.id=ref_ans.id
JOIN us_states on ref.id=us_states.id
WHERE ref_ans.q_id = <id>