Write A Select Query For Getting Table Value Using Another Table Field Value
I am having two tables student and guardian table So I want to write a query to get the guardian name guardian age from guardian using stu_uid retrievd from student table using stu
Solution 1:
Instead, write your query as below
Select `guardian_nm`, `guardian_age` from `guardian` where `stu_uid` = 1;
Since your student id is static use it directly on the query instead of writing subqueries
or else you can use inner joins, to learn more about inner queries follow the link below
https://www.w3schools.com/sql/sql_join_inner.asp
Try this for inner join,
SELECT `guardian`.`guardian_nm`, `guardian`.`guardian_age`, `guardian`.`stu_uid`
FROM `guardian`
INNER JOIN `student` ON `guardian`.`stu_uid` = `student`.`stu_id`
WHERE `student`.`stu_id` = '1';
Solution 2:
Use join in your query, you have not provided the table structure, so I have to guess, your query may be like this or similar to this depending on the table structure.
SELECT `a`.`guardian_nm`, `a`.`guardian_age`
FROM `guardian` `a`
INNER JOIN `student` `b`
ON `a`.`stu_uid` = `b`.`stu_uid`
WHERE `b`.`stu_id` = '1';
Post a Comment for "Write A Select Query For Getting Table Value Using Another Table Field Value"