tbl teacher
lrn
fname
lname
email
image
schedule
tbl user
lrn
fname
lname
email
image
account-type
$id=$_SESSION['id'];
$get_record_sched = mysqli_query ($db, "SELECT schedule FROM teacher INNER JOIN users ON teacher.lrn = users.lrn WHERE teacher.lrn = '$id' ");
I want to echo the exact schedule based on the id of the logged in account. But with this, query, there's no output...
You said you have $id from the session. Assuming that column lrn is the ID, then
Select t.schedule
From teacher t
Where t.lrn = $id
No need to join the table with the user table.
Note that parameter binding is advised.
$statement = $connection->prepare("Select t.schedule From teacher t Where t.lrn = ?");
$statement->bind_param("s", $id);
In you where condition was wrong.They don't have any param to validate condition.so change like this
"SELECT schedule FROM teacher INNER JOIN users ON teacher.id = users.id WHERE 1 "
OR
use same where condition with join
"SELECT schedule FROM teacher INNER JOIN users ON teacher.id = users.id AND teacher.schedule = users.id WHERE 1 "
OR
if You have logged used id in session like
$id=$_SESSION['user_id'];
"SELECT schedule FROM teacher INNER JOIN users ON teacher.id = users.id WHERE teacher.schedule='$id' "
Related
How do i join two fields from table1 to table 2?
I have this code below, how do i also join fields "mobilenumber","firstname" and "lastname" from the user table into the user_address table?
$query = "SELECT * FROM user_address WHERE user_id IN
(SELECT id FROM user WHERE email = '".$email."')";
$query = "SELECT a.*,b.mobilenumber,b.firstname,b.lastname
FROM user_address a
left join user b on a.user_id=b.user_id
WHERE a.user_id IN
(SELECT id FROM user WHERE email = '".$email."')";
You can use LEFT JOIN instead and select attributes from both table. Example:
$query = "SELECT ua.*,
u.mobilenumber,
u.firstname,
u.lastname
FROM user_address ua
LEFT JOIN USER u
ON u.id = ua.user_id
WHERE u.email = '$email'";
First I want to write a join query to get every doc and the subjects that he uploaded i used this query but it is not working well and showing all the subjects under one doctor and all i have to get the subjects of the course is the course ID
SELECT users.ID
, fullname
, Sub_ID
, Sub_name
, Sub_ext
, Sub_path
, subject.created_at
FROM users
JOIN subject
ON users.ID = subject.ID
WHERE C_ID = '$C_ID'
Your query is wrong...
Correct is...
"SELECT u.ID,u.fullname,s.Sub_id,s.Sub_name,s.Sub_ext,s.Sub_path,s.Created_at
FROM users u LEFT JOIN subject s
ON u.ID=s.ID
WHERE s.C_ID='.$C_ID.'";
i solved it by using 2 queries
first one to get doc in that course and the second one from the subjects that doctor give
$query = "SELECT `doc-course`.ID,fullname
FROM `doc-course`
JOIN users ON `users`.`ID` = `doc-course`.`ID`
WHERE C_ID='$C_ID' ";
the second
$query2 = "SELECT Sub_ID,Sub_name,Sub_ext,Sub_path,`subject`.`created_at`
FROM subject
WHERE `subject`.`ID` = '$ID[$x]'
";
I have two mysql tables-members and addclique. Members table contains the details of all users while addclique table is 'friends' table with two columns-adder_id, which contains id of the user that sent a friend request and clique_id, contains the id of user that accepted the friend request.
Please am trying to select the friends of a particular user but am finding hard getting the details (photo, name, etc) of the friends from the members table because I don't know at which point to join members table to the addclique table.
$sql = "SELECT i.*, m.* FROM addclique i JOIN members m ON m.id = ******** WHERE adder_id = :id OR clique_id = :id";
$result= $db->query($sql, array('id' => $_SESSION['id']);
The query below will select all members that a particular person has added plus all members that have added the same person.
select m.* from members m
join addclique a on m.id = a.adder_id
where a.clique_id = :id
union
select m.* from members m
join addclique a on m.id = a.clique_id
where a.adder_id = :id
If I'm understanding your question correctly, you need to join on m.id = a.adder_id or m.id = a.clique_id. You can do this with in:
set #id := 4;
select *
from members m
join addclique a on m.id in (a.adder_id, a.clique_id)
where #id in (a.adder_id, a.clique_id)
and m.id != #id
SQL Fiddle Demo
I want to do a contacts page. Here is my table structure:
Accounts Table:
1) id
2) User_id
3) Full_name
4) Username
5) Email
6) Password
Contacts Table:
1) My_id (This is the user who added friend_id)
2) Contact_id (this is the contact who was added by my_id)
3) Status
My query looks something like this:
$sid = ID of user who is viewing;
$sql = "SELECT STRAIGHT_JOIN DISTINCT contacts.contact_id,
accounts.full_name FROM contacts INNER JOIN accounts
on contacts.contact_id = accounts.user_id WHERE
contacts.my_id = '$sid' OR contacts.contact_id = '$sid'";
The problem is that it doesn't work the right way. I end up seeing my name in the query (meaning when i login i see my name in the contacts instead of contacts name).
How to fix this? Thanks.
The STRAIGHT_JOIN keyword should not be necessary here. To get the contact information, use a second JOIN to the contacts table which relates it back to the accounts.
SELECT
DISTINCT c.contact_id,
cn.full_name
FROM
accounts a
/* first join connects the adding user's id to records in contacts */
INNER JOIN contacts c ON a.user_id = c.My_id
/* second join connects contact list user ids back to names in accounts */
INNER JOIN accounts cn ON cn.user_id = c.Contact_id
WHERE a.User_id = '$sid'
This query ought to suffice:
SELECT DISTINCT contacts.contact_id, accounts.full_name
FROM contacts, accounts
WHERE (contacts.my_id = '$sid' AND contacts.contact_id = accounts.user_id)
OR (contacts.contact_id = '$sid' AND contacts.my_id = accounts.user_id)
Yes so im building an query from the advanced search form.
I have this:
$query = "SELECT * FROM users WHERE 1 ";
$query .= "AND sex = '$sex' ";
for now, next im going to have AND birthday.. but then i dont know how to do it, because users birthday is stored in users_profile
So please correct me, how can i:
$query .= "AND birthday in users_profile = '1'";
Is this possible even, or should i reconstruct it and put birthday in users instead..
update:
in users, the id column there, is binded with users_profileĀ“s uID.
So in users_profileĀ“s uID column, there is the users id.
I assume your users_profile table is linked to the users table?
SELECT u.*, up.birthday
FROM users u
INNER JOIN users_profile up
ON u.user_id = up.user_id
WHERE sex = '$sex'
Here an Inner Join is used. The reason we can use u instead of users and up instead of users_profile is because we have set up the aliases "users u" and "users_profile up"
You need to look at the syntax for JOIN.
You need a way to join individual related rows in the two tables, something like:
SELECT u.* FROM users u, users_profile p
WHERE u.sex = 'M'
AND p.birthday = '1'
AND u.userid = p.userid;
I don't understand why you have separate tables for user and for users_profile, but you need to JOIN the two tables:
SELECT U.*
FROM users U
LEFT JOIN users_profile P
ON P.uID = U.uID
AND P.birthday = '1'
WHERE U.sex = '$sex'
Very possible, given you have the foreign key to the users_profile table.
Let's say the primary key in the users table is named 'id', and the users_profile table contain a field called 'uid' which point to the users table, you'd normally create the query like this:
SELECT * FROM users u, users_profile p WHERE u.id = p.uid
AND u.sex = '$sex' AND u.birthday = 1