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
Related
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
);
I am trying to query two tables: finished_events and flagged_events. 1st of all I need everything related to the company_id so
SELECT *
FROM finished_events
WHERE company_id=$id
ORDER by schedule, timestamp
I then changed this to:
SELECT * FROM finished_events
INNER JOIN flagged_events
ON finished_events.company_id=flagged_events.company_id
WHERE finished_events.company_id=$id
ORDER by finished_events.schedule, finished_events.timestamp
I have tried using FULL JOIN, LEFT JOIN, and RIGHT JOINs all unsuccessful. Specifically what I want is to get is a combined effort of the following code:
$sql = "SELECT *
FROM finished_events
WHERE company_id=$id
ORDER by schedule, time_stamp";
$flagged_sql = "SELECT *
FROM flagged_events
WHERE company_id=$id
ORDER by schedule, time_stamp";
The tables are a bit different so UNION won't work here. I can post dummy database entries but this won't be of too much help as I need all from both tables. The 2 links between the tables would be the company_id and the schedule columns. Essentially what is going on behind the scenes is timestamps being put into a different table to which I then process either into finished_events or flagged_events. Flagged events will need the user to do something about it until it is a finished event. So this script is generating the data for the GUI, hence why I need to query both tables and create an associative array of customer details then an array of events (from these 2 tables). So creating the assoc_array is no problem I just need to get this query to spit out all the events and order them correctly. Let me know if you need anything specific to solve this one, thanks :)
EDIT
SQL Fiddle: http://sqlfiddle.com/#!2/d4c30/1
this almost fixes it but not quite right, it repeats entries at the bottom
If I understood correctly, this may be useful for you:
SELECT a.* FROM (
SELECT *, 'finished' as event_type FROM finished_events
UNION
SELECT *, 'flagged' as event_type FROM flagged_events) a
ORDER BY a.schedule, a.time_stamp
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
I have 2 mysql tables
one has a list of user ids that are associated with a city. ie "Fort Lauderdale" but the user id is actually in a column called entity_id and the city is in a field called field_city_value.
This query brings back all of the entity_ids in "Fort Lauderdale"
SELECT entity_id
FROM `field_data_field_city`
WHERE `field_city_value` LIKE 'Fort Lauderdale'
and then this query brings back the mail for the user id
SELECT mail
FROM `users`
WHERE `uid` =42
I want to combine the 2 and get all of the mails for all of the user ids that match Fort Lauderdale.
Use a join statement.
http://dev.mysql.com/doc/refman/5.0/en/join.html
Heres the long winded way of doing it.. Untested, no mysql access on this box.
SELECT field_data_field_city.entity_id,users.mail FROM users,field_data_field_city WHERE field_city_value LIKE 'Fort Lauderdale' AND field_data_field_city.entity_id = users.uid
Or
SELECT * FROM field_data_field_city city INNER JOIN users user on city.entity_id=user.uid
Can we assume that users.uid is the same as field_data_field_city.entity_id? If that's the case you'll want to look into using MySQL joins
You can try a subselect:
SELECT mail FROM users WHERE uid IN (SELECT entity_id FROM field_data_field_city WHERE field_city_value LIKE 'Fort Lauderdale')
This should do it. You might want to change it a little bit for duplicates or similar, but this simple query should work
SELECT u.mail
FROM users u
LEFT JOIN field_data_field_city fdfc ON fdfc.entity_id = u.uid
WHERE fdfc.field_city_value LIKE 'Fort Lauderdale'
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`