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]'
";
Related
I've tried to get data from from one table if id of 2 tables equals to each other. Here is the code which I used:
SELECT id_to
, email_to
, name_to
, status_to
FROM users
LEFT
JOIN friends
ON users.id = friends.id_from
WHERE id_from = ?
I used LEFT JOIN to join two tables but it gets the values from the friends(table) instead of users(table).
I think I've explained my problem clearly.
I guess you must specify it on your query, like this:
SELECT users.id_to, users.email_to, users.name_to, user.status_to FROM users LEFT JOIN friends ON users.id = friends.id_from WHERE id_from = ?
You can do the same if you need to retrieve values from 'friends' table.
If both tables have the same column, then you can specify the table name while selecting columns. so your code will look like:
SELECT users.id_to, users.email_to, users.name_to, user.status_to FROM users LEFT JOIN friends ON users.id = friends.id_from WHERE friends.id_from = ?
My tables
$sql="SELECT *
FROM addresses
LEFT JOIN users ON address_id = user_id
LEFT JOIN notes ON note_id = user_id
ORDER BY id DESC
LIMIT 1";
This is my SQL query, my task is to show the last records from 3 tables, but the table is blank, I don't know why,thanks in advance people :)
I guess the problem is coming from the ORDER BY id DESC .
Indeed, you have no column so called id.
You should probably remove this clause, in order to make your code work.
If you want to take the last records anyway, you can put an ORDER BY address_id DESC which will do the job !
The code directly edited :
$sql="SELECT *
FROM addresses
LEFT JOIN users ON address_id = user_id
LEFT JOIN notes ON note_id = user_id
ORDER BY adress_id DESC
LIMIT 1";
This may work:
SELECT a.address_id, u.user_id, n.note_id
FROM addresses a
LEFT JOIN users_addresses ua ON ua.ua_address_id = a.address_id
LEFT JOIN users u ON u.user_id = ua.ua_user_id
LEFT JOIN notes n ON n.note_user_id = u.user_id
ORDER BY a.address_id DESC
LIMIT 1
Here is the query to get all data from all the tables, not sure what do you mean last records from 3 tables, I can see four tables there:
SELECT *
FROM `addresses`
LEFT JOIN `users_addresses` ON `users_addresses`.`ua_address_id` = `addresses`.`address_id`
LEFT JOIN `users` ON `users`.`user_id` = `users_addresses`.`ua_user_id`
LEFT JOIN `notes` ON `notes`.`note_user_id` = `users`.`user_id`;
i need help getting data from different tables and insert into other different table Here are the Queries
"SELECT commentID, date, comment, subject, parentID, aBUserID FROM comments WHERE status = 'APPROVED'"
"SELECT topicID, subForumID, aBUserID, lastPostID, views, replies, startDate FROM topic WHERE status = 'APPROVED' AND topicID = $parentid";
// $parentID need to be matched from above query parentID,
"SELECT userName FROM users WHERE aBUserID = $cmtaBUserID";
// $cmtaBUserID = aBUserID from first query
"SELECT userName FROM users WHERE aBUserID = $topicaBUserID";
//$topicaBUserID = aBUserID from second query
Last 2 queries are from same table but using different where clause
i used different inner join left join from solutions posted here but non of these worked for me stuck since last 2 weeks please help
PS data from all above Queries will be inserted to a single table i need these to be combined so i can have them all in one place
If you want to perform the operation in same query use 'OR'
"SELECT userName FROM users WHERE aBUserID = $cmtaBUserID OR aBUserID = $topicaBUserID";
Please try this
SELECT userName from users where aBUserID IN(SELECT aBUserID FROM comments WHERE status = 'APPROVED')
Couldn't test it but Maybe this is what you are looking for.
SELECT c.commentID, c.date, c.comment, c.subject, c.parentID, c.aBUserID,
t.topicID, t.subForumID, t.aBUserID, t.lastPostID, t.views, t.replies, t.startDate,
u.userName
FROM
comments c
left outer join topic t on t.topicID = c.parentID
left outer join users u on u.aBUserID = c.aBUserID and u.aBUserID = t.aBUserID
WHERE
c.status = 'APPROVED' and t.status = 'APPROVED';
try this:
SELECT
comment.[commentID],
comment.[date],
comment.[comment],
comment.[subject],
comment.[parentID],
comment.[aBUserID],
commentuser.[userName],
topic.[topicID],
topic.[subForumID],
topic.[aBUserID],
topic.[lastPostID],
topic.[views],
topic.[replies],
topic.[startDate],
topic.[userName]
FROM comments comment
LEFT OUTER JOIN users commentuser
ON commentuser.aBUserID = comment.[aBUserID]
LEFT OUTER JOIN
(
SELECT
t.[topicID],
t.[subForumID],
t.[aBUserID],
t.[lastPostID],
t.[views],
t.[replies],
t.[startDate],
u2.[userName] --user from users table joined to topics table
FROM topic t
LEFT OUTER JOIN users u
ON u.aBUserID = t.[aBUserID]
WHERE t.[status] = 'APPROVED'
) topic
ON topic.topicID = comment.parentID
WHERE comment.[status] = 'APPROVED'
I have two tables firm and contactdetails. I am trying to get the the firm name from firm and certain contact details from contactdetails. I am using $id =$_GET['id']; to get the id . In contactdetails i have fk_firm_id which is my foreign key. I am not sure how to use the inner join query. I am trying the following query:
$sql="SELECT firm.`name` ,`address_physical_line_1` , `fax_1` , `phone_1`
FROM contactdetails JOIN firm ON contactdetails.fk_firm_id='$id'";
echo $sql;
$result = mysql_query($sql);
but i am not getting the correct firm. Can anyone help me with this query, please.
You should use like this JOIN firm ON contactdetails.fk_firm_id = firm.id
$sql=" SELECT firm.`name` ,`address_physical_line_1` , `fax_1` , `phone_1`
FROM contactdetails
JOIN firm ON contactdetails.fk_firm_id = firm.id
WHERE contactdetails.fk_firm_id = '$id'
";
$result = mysql_query($sql);
This is assuming that your firm table has a primary key called id
$sql="SELECT firm.`name` ,`address_physical_line_1` , `fax_1` , `phone_1` FROM contactdetails JOIN firm ON `contactdetails`.`fk_firm_id`=`firm`.`id`
WHERE `firm`.`id` = '$id'";
echo $sql;
$result = mysql_query($sql);
There is a mistake about JOIN and WHERE statements:
$sql = "SELECT
f.name,
c.address_physical_line_1,
c.fax_1,
c.phone_1
FROM
contactdetails c JOIN firm f ON c.fk_firm_id= f.id
WHERE c.id = '$id'";
$sql="SELECT firm.`name` ,`address_physical_line_1` , `fax_1` , `phone_1` FROM
contactdetails JOIN firm ON contactdetails.fk_firm_id=firm.id where
contactdetails.fk_firm_id='$id'";
you should join on a firm's field such as firm.id
syntax: FROM table1 LEFT JOIN table2 ON table1.field1 compopr
table2.field2 compopr is : "=","<",">","<=",">=","<>"
You are missing the WHERE clause that limits the result set to only the firm you're interested in; now you're getting all firms joined with a single contact details record.
.. where firm.id=$id
For new applications, please use a database API that has prepared statements, like mysqli or pdo.
Use the following query for inner join
$sql="SELECT firm.name ,address_physical_line_1 , fax_1 , phone_1 FROM
contactctdetails INNER JOIN firm ON contactdetails.fk_firm_id=$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