I need to filter data based on one table, but i dont know the correct usage for the select from function in php mysql. The below code shows what i am trying to do, but I think you can use multiple SELECT.
$query_Student = "SELECT Project_Title, Project_Lecturer FROM projects WHERE Project_id =
(SELECT Proj_id FROM project_course WHERE Cour_id = (SELECT Course_id from courses WHERE
Code = (SELECT Course FROM users WHERE Username = ".$_SESSION['MM_Username']."))"
Can anyone offer any assistance to what I need to write instead?
What you want to do is not exactly clear to me, but to make your query work you need to put single quotes around $_SESSION['MM_Username'], like gogowitsch already said.
Also another closing paranthese is missing. So your query should look like this:
$query_Student = "SELECT Project_Title, Project_Lecturer FROM projects WHERE Project_id =
(SELECT Proj_id FROM project_course WHERE Cour_id = (SELECT Course_id from courses WHERE
Code = (SELECT Course FROM users WHERE Username = '".$_SESSION['MM_Username']."')))"
But...
That is not a very well written query.
I'm just guessing now, but maybe this is what you are looking for:
SELECT
p.Project_Title,
p.Project_Lecturer
FROM projects p
INNER JOIN project_course pc ON p.Project_id = pc.Proj_id
INNER JOIN courses c ON pc.Cour_id = c.Course_id
INNER JOIN users u ON c.Code = u.Course
WHERE u.Username = 'yourUserName';
If not, show us the tables you are using (do a SHOW CREATE TABLE projects; and so on for every table you are using and post it here) and maybe what output you expect, then we can really help.
You might need to add quotes around the username. Can $_SESSION['MM_Username'] be a string?
Related
SELECT user.name, comments.cdata, comments.likes FROM comments
WHERE pid = $postNum
INNER JOIN user ON comments.uid = user.uid
ORDER BY cdate
Quick Notes:
I am a beginner, please don't be rude to me, I am trying to learn more
Yes, I have tried LEFT JOIN, but that just returns an SQL sintax error
My database is like this:
2 tables, 1 one is comments, comments has comments.cdata, comments.likes and comments.uid, the user one has the name of the user.
What I have been trying to accomplish is getting the name of the user with the comment data, instead of UID and comment data.
I also can not use 2 queries, due to me getting all the records and then displaying them on page via PHP for each.
Your query is syntactically incorrect. JOIN is an operator in the FROM clause. WHERE is a clause that follows the FROM clause.
In addition, I think the cdata and cdate should be the same thing, although I don't know what.
I also recommend using table aliases. So:
SELECT u.name, c.cdata, c.likes
FROM comments c JOIN
user u
ON c.uid = u.uid
WHERE c.pid = $postNum
ORDER BY c.cdata
I'm using the following query to pull data from 3 tables. 2 are in the same database and 1 in a different database. Basically I have recipes in one table, and I'm using the recipieToCountry table to connect to the countries table so I know what country a recipe belongs to.
$contentQuery = $page->dbf->query("
SELECT *
FROM recipes, recipeToCountry, content.countries
WHERE recipes.id = recipeToCountry.recipeId
AND recipeToCountry.countryId = content.countries.id
AND content.countries.origId = '$country'
");
The problem I'm running into is when I call $content->title it returns the title from countries when I want the title from recipies. My understanding is SELECT * is the issue, so I tried to change the query to this:
SELECT title, description, approved, active, id, recipeId, countryId
FROM recipes, recipeToCountry
WHERE recipes.id = recipeToCountry.recipeId
UNION
SELECT id, origId, null, null, null, null, null
FROM content.countries
WHERE content.countries.origId = 1
AND recipeToCountry.countryId = content.countries.id
Unfortunately this query has an error, and I'm unsure how to fix it. When I removed the last AND the UNION doesn't seem to work as well. Any ideas on what changes I could make to get the correct columns?
Simple JOINs should work well:
select r.*, c.*
from recipes r
join repiceToCountry rc on r.id = rc.recipeId
join content.countries c on c.id = rc.countryId
where c.origId = '$country'
You must make sure you have SELECT privileges on the other database.
Also, you can replace r.*, c.* by the specific columns you want to show.
I developed a habit a long time ago of always putting the table alias/name before every field in the query. This way you can modify the query later on without worrying about fields names that conflict when you add tables to the query. I also prefer joins to multiple FROM tables. (It's easier to see the logic)
I would change your first query to this:
SELECT r.*, c.country /* any other fields in country besides title */
FROM recipes as r
LEFT JOIN recipeToCountry as rtc ON rtc.recipeId = r.id
LEFT JOIN content.countries as c ON c.id = rtc.countryId
WHERE c.origId = '$country'
You can leave the word "as" out to make it look a little cleaner, but I leave it in there to show what's happening a little better.
So, I am trying to select some data from 4 tables using a query I have attempted to throw together.
SELECT *
FROM cards
LEFT JOIN cards_viewers ON cards.card_id = cards_viewers.card_id
(SELECT *
FROM folders
WHERE folder_id = cards.card_folderID)
(SELECT user_firstName,
user_lastName,
user_avatar
FROM user_data
WHERE user_id = cards_viewers.user_id)
WHERE cards_viewers.user_id = '.$u_id.'
ORDER BY cards.card_lastUpdated DESC
Basically, the query selects data from the four tables depending on the user_id in table user_data. I have attempted to initially fetch all data from the tables cards, and cards_viewers, and have went on to use this data to select values from the other tables (user_data and folders).
The query is wrong, I know that. I have learnt the majority of basic MySQL, but I am still struggling with more complex queries like the one I am trying to write now. What query can I use to select the data I want?
Links to any documentation to parts of queries would prove very useful in helping me learn how to create queries in future, rather than just relying on StackOverflow.
Many thanks.
You don't need "MULTI-WHERE" but multiple joins, you just need to keep doing joins until you get the tables you need.
Here's an example:
SELECT *
FROM cards LEFT JOIN cards_viewers
ON cards.card_id = cards_viewers.card_id
LEFT JOIN folders
ON folders.folder_id = cards.card_folderID
LEFT JOIN user_data
ON user_id = cards_viewers.user_id
WHERE cards_viewers.user_id = '.$u_id.'
ORDER BY cards.card_lastUpdated DESC
To custom the fields you want to get just change * for the name of the field being careful about ambiguous column naming.
For further information check MySql Joins. Hope this helped you :)
I have two SQL tables. The first one structure is simple.
ID Name
----------
The second one is simple too.
CommentID Comment CreatorID
----------------------------------
I want to show in my site all the comments that correspond with the "ID of user"
The problem is we have here a lot of ID and two different tables.
Something like this:
$1= $bdd->query("SELECT * FROM comments WHERE id_user=220281");
$2=$1->fetch();
But its impossible because id user is not on the comments table.
The most simple way to do this is to join the tables like this:
select
users.name,
comms.commentID,
comms.comment
from
userTable users
join commentTable comms
on users.ID=comms.ownersID
where
users.id=1
This will return the users name in each row of data, but you don't need to use it in the output more than once.
It also sounds like you could use a few pointers on SQL queries. Do yourself a favour and have a read of this article I put together a while back.
SELECT c.*
FROM comments c
INNER JOIN users u ON c.id_creator = u.id_user AND
u.id_user = 220281
A simple join will do the trick like this :
SELECT c.comment, u.user_name FROM
Users u
JOIN
Comments c ON
c.creator_id = u.user_id
WHERE
u.user_id=220281
fiddle:http://sqlfiddle.com/#!6/3b28a/1
I am somewhat new to coding and have been trying to write what I thought would be a straightforward sql query. Please help :)
Table 1: Users
id = user idt
username = user name
Table 2: Orders
orderid = order id
order_to = user id of person buying
order_from = user id of person selling
oder_details = text
Basically I want to:
"Select Username(from), Username(to), order_details FROM mytables WHERE Order id = 1;"
And get the result as 1 row, I'm not sure how to proceed. I thought I could do this with concatenation or something... Can anyone help?
You need to use JOIN to link the tables together.
SELECT fu.username AS fromUser, tu.username AS toUser, o.order_details
FROM Orders o
INNER JOIN Users fu
ON fu.id = o.order_to
INNER JOIN Users tu
ON tu.id = o.order_from
WHERE o.orderid = '1';
Because you have two different users that you need the username from, you need to JOIN the Users table twice to get both user's usernames. Each table needs to have it's own alias fu and tu to allow MySQL to differentiate between them. The same goes for the column names in your SELECT statement so that when you fetch the results with PHP, php can differentiate between the two usernames.
You are looking for a JOIN. This can be done with a keyword or through WHERE clauses. For example,
SELECT * FROM Orders JOIN Users ON Orders.order_to=Users.id
The documentation can be found here: http://dev.mysql.com/doc/refman/5.0/en/join.html.
I'll leave it as an exercise to figure out how to JOIN the order_from.