OK, here's yet another sql query that's giving me headaches.
Case: I'm listing a description of an auction, and the listing will also say whether the user who is logged in is watching this auction, or not (like a favourite).
If he's watching the auction, his user ID + auction ID is inside a table called watchlist-auctions. If he's not watching, the entry is not there.
Here is my query:
SELECT
`auctions`.`auction_description_1`,
`watchlist-auctions`.`watchlist_a_id` as `watch-auction`
FROM `auctions`
LEFT JOIN `watchlist-auctions` ON `auctions`.`auction_id`=`watchlist-auctions`.`watchlist_a_auction`
WHERE `auction_id`='6'
GROUP BY `auctions`.`auction_id`
Basically, it works, kinda, but I don't know where in the query do I define the user ID?
I have the logged in user id in $userID (in php).
I'm not sure whether I explained this right, I'm so depressed from this, because it's such a simple problem, but I find those SQL queries so difficult I feel like crying. If I can explain this better, please, tell me where I messed up :/
P.S.: If I can offer a bounty for this, I want to offer +100 right away.
SELECT
`auctions`.`auction_description_1`,
`watchlist-auctions`.`watchlist_a_id` as `watch-auction`
FROM `auctions`
LEFT JOIN `watchlist-auctions` ON
`auctions`.`auction_id`=`watchlist-auctions`.`watchlist_a_auction`
AND `watchlist-auctions`.userID= $userID
WHERE `auction_id`='6'
GROUP BY `auctions`.`auction_id`
So the LEFT JOIN activates on both conditions auction+user, but it does not interfere with the LEFT table (auctions).
Not completely sure where the user_id field is located in your schema, also not sure what the field name is in the table; but you should be able to add it as additional criteria to your where clause.
WHERE `auction_id`='6' AND `watchlist-auctions`.`user_id` = $userID
I'm not sure why you're left joining as every auction should be in the watch-auctions:
SELECT
`auctions`.`auction_description_1`,
`watchlist-auctions`.`watchlist_a_id` as `watch-auction`
FROM `watchlist-auctions`
inner JOIN `auctions` ON `auctions`.`auction_id`=`watchlist-auctions`.`watchlist_a_auction`
WHERE `auction_id`='6'
and `user_id` = .......
GROUP BY `auctions`.`auction_id`
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 want to update a field in my table based in another table and I executed this query below but I think it's not right.. it looks like it worked but is it correct? Is there any situation where it might fail?
UPDATE users SET page = (SELECT page_name FROM pages WHERE user_id = id)
My table USERS has a column id and page. My table PAGES has a column page_name and user_id. Is the code above right?
It may fail if pages has more than one page_name per user_id. I find UPDATE a INNER JOIN b ON some_conditions SET a.fieldA = b.fieldB; to be much more readable. It does have the same failure scenario, and can be harder to "fix" for such scenarios; but correlated subqueries (your version) tend to be significantly slower.
Also, style note, UPDATE users AS u SET u.page = (SELECT p.page_name FROM pages AS p WHERE p.user_id = u.id); would've eliminated the need for your last two sentences and (more importantly):
make it so the next developer that has to look at the query does not have to look at the database to find out (or remember) what fields go to what tables.
make it so the query does not break if an id field later gets added to pages.
Instead of subselect In mysql you can use UPDATE JOIN
UPDATE users
INNER JOIN pages on pages.user_id = users.id
SET users.page = pages.page_name
Whenever you have more than one table in a query, you should always use qualified column names -- and preferably aliases. So, your version of the query would be:
UPDATE users u
SET page = (SELECT p.page_name FROM pages p WHERE p.user_id = u.id);
Next, you have to consider whether the subquery might return more than one row. If so, you have to limit it to one row. There are various ways, SELECT MAX(p.page_name), LIMIT 1, and SELECT GROUP_CONCAT(p.page_name) all come to mind.
Next, you are updating all rows in users. If you only want to update matching rows, then you can continue on the subquery path using IN or EXISTS in the WHERE clause. Alternatively, use JOIN:
UPDATE users u JOIN
pages p
ON p.user_id = u.id
SET u.page = p.page_name;
But most importantly, ask the existential question: Why do you need to do this update? You have a link between the two tables. Use the link instead of storing the name:
select u.*, p.page_name
from users u left join
pages p
on p.user_id = u.id;
You can use the below sample SQL and change as per your requirement.The Code above seems correct .Could you please paste the error
UPDATE TableB
SET TableB.value = (
SELECT TableA.value
FROM TableA
WHERE TableA.name = TableB.name
);
Ok, I have two tables of information: movies & history. My movies db has all the information about the movies. These are the rows in movies: id, title, photo, destination, description, rating, length, likes.
My history db has all the information about what movies the user has clicked on. These are the rows in history: id, user_id, movie_id.
The Question
I'm working on a feature called "My History." How do I show their history but also get the information from the movies in one SQL query?
For example:
$db->query("SELECT * FROM movies ORDER BY TABLE history WHERE user_id='$id'");
I apologize if the answer seems simple. This is my first year working with programming in general. Thanks for any help give.
Your query would be:
SELECT * FROM movies m
INNER JOIN history h ON h.movie_id = m.id
WHERE h.user_id = $id
ORDER BY h.id --not sure what you want to order by, but it goes here
That being said, you need to be aware of SQL Injection. You should be using prepared statements instead of just adding variables directly into your query.
You have some error in your query SQL.
I think you can try: (in Mysql)
SELECT movies.* FROM history INNER JOIN movies ON history.movie_id = movies.id WHERE user_id = $id
What you are looking for is a JOIN in MySQL. You'll want to do something similar to this:
SELECT * FROM movies m
JOIN history h ON m.id=h.movie_id
AND h.user_id = $id
ORDER BY <column name>;
Basic concept behind this JOIN is you are taking the primary key from movies (id) and joining on its corresponding foreign key on history (movie_id).
It's also worth noting that JOIN, CROSS JOIN, and INNER JOIN are all syntactic equivalents in MySQL.
Since you are just starting your first year in programming, I would highly recommend looking in to the documentation on JOIN and fully understanding it yourself, rather than copying and pasting an answer that works here. It will be beneficial to you in the long run! See the MySQL documentation on JOIN syntax here.
I got two tables :
A list of people;
A list of people I want to ignore.
When I read the list of people, I don't want to see the ignored people in the list.
My current solution is to query a second time the database (to select the people I want to ignore) and remove them from the array I create with PHP. It's working and it's fine.
However, I want to do that in MySQL. I know JOIN will join only if the row exists in the other table. I am looking for something different (won't show the entry IF the row exists).
I have searched in Google but the lack of "keywords" for this gave me no results.
Thanks
SELECT * FROM Person
LEFT OUTER JOIN IgnoredPerson
ON Person.id = IgnoredPerson.id
WHERE IgnoredPerson.id IS null
Explanation:
Exclude the records we don't want from the right side via a where clause
http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html
Without knowing your schema, I'd suggest something along these lines:
SELECT * FROM people WHERE id NOT IN (SELECT person_id FROM ignored_people)
You could try something like this
SELECT * FROM people p WHERE NOT EXISTS (SELECT i.id FROM ignorePeople i where p.id = i.id )
here's a link about EXISTS in MySql
I'm having trouble with a join query, my issue is as follows.
Table: battles
Fields: id,attacker_id,defender_id
Table: users
Fields: id,profile_image
I would like to do a query to retrieve a battle and get the profile images as well from the other table.
Is there a way to do this in a single or do I have to do more than one?
Thanks in advance.
I wanted to wait a while to see if you had any attempt or if you will answer my first question to know if I understood the problem. But maybe you don't have a starting point. Try something like:
SELECT
a.profile_image as attacker_profile_image,
d.profile_image as defender_profile_image
FROM
`battles` b
LEFT JOIN
`users` a
ON
b.`attacker_id` = a.`id`
LEFT JOIN
`users` d
ON
b.`defender_id` = d.`id`
the problem here is the fact that you need to join with the users table twice, so you will need to create aliases for the columns you plan to use
This query will fetch the two images only, you will need to add the extra fields