I have two tables, one is where the actual posts are, and one for posts that are saved.
I want to delete a row from the saved table if a post is deleted from the website
I tried this:
mysql_query("DELETE FROM `saved` WHERE (SELECT * FROM `ads` WHERE ad_id = `saved`.ad_id) LIMIT 1");
but this doesn't work. Can't think of how to do it the right way
any help it appreciated!
This will delete all rows from saved which its saved.ad_id do not reference to any ads.ad_id
delete s
from saved AS s
left outer join ads as a
on a.ad_id=s.ad_id
where a.ad_id is null;
More here: MySql delete syntax
And here: MySql Multi delete example
you can use left outer join to get the rows exist in table 1 and not exist in table 2 then you can delete it from table 2 using their IDs
mysql_query("DELETE FROM `saved` WHERE `saved`.ad_id not in (SELECT ad_id FROM `ads`)");
Related
I have two similar table in my database. I want update my database called qu_time in tbl_quotes from table new_quotes.
I have tried query like this:
UPDATE tbl_quotes
SET qu_time = (SELECT qu_time FROM new_quotes)
but I get an error
1242 - Subquery returns more than 1 row
Let me know if someone have idea to solve it.
Thanks
You should have atleast one common column in both tables.
Use that column in the join condition and do the updating..
UPDATE tbl_quotes t1
JOIN new_quotes t2
ON t1.Id_column = t2.Id_column
SET t1.qu_time= t2.qu_time
I have 3 tables. 1 table is like the master table and I want all rows from this table where GameID = X. Then I have a guides table which will have a matching ID and finally i have a user table that defines whether the user has selected this row to be hidden. this is causing issues. This table may not have a row associated with it. This table is shared amongst ALL users. The primary key of this table is UserID+InfoID. The query below returns what I want provided there are no other rows in the table for other userIDs.
SELECT PS_Info.*, PS_Guides.Guide, PS_Userhidden.* FROM PS_Info
LEFT JOIN PS_Guides ON PS_Info.ID = PS_Guides.InfoID
LEFT JOIN PS_Userhidden ON PS_Info.ID = PS_Userhidden.InfoID
WHERE PS_Info.GameID = :ID AND (PS_Userhidden.UserID = :UserID)
OR (PS_Userhidden.UserID IS NULL AND PS_Userhidden.InfoID IS NULL)
So I will run the php script and have infoID =1 and userID=1. In the table there is infoID=1 and userid = 2, but nothing will be returned for this row. If I remove PS_Userhidden.UserID = :UserID I get multiple of the same row. The user table will grow to millions of rows. I need a way to make this query stick to the primary key of the users table so it will still return a row if no match exists in the user table and also return a row if there is a match in the users table for the specific user
I think you just need to move the condition on the hidden user to the ON clause:
SELECT i.*, g.Guide, h.*
FROM PS_Info i LEFT JOIN
PS_Guides g
ON i.ID = g.InfoID LEFT JOIN
PS_Userhidden h
ON i.ID = h.InfoID AND h.UserID = :UserID
WHERE i.GameID = :ID ;
Your description of the problem sounds like something that can happen when you start fiddling with conditions in the WHERE clause of a LEFT JOIN. It is a little hard to follow though. If this doesn't work, edit your question with sample data and desired results -- or, better yet, set up a SQL Fiddle.
I use php and mysql. I have two tables,
table A (Id: auto-increment , idno)
table B (Id:auto-increment, sidno).
Table A contains about 3000 records and Table B contains about 27000 records. I want to search whether each of the records in table A exist in table B, if not print the records that does not exist in table B.
I tried to retrieve the records in table A and checking them against table A, but I could not succeed. And it took a very long time to finished the query.
And I have searched throughout but could not get something like this.
Please can anybody help me.
Thanks!
The following query might return all the idno which are not in table B
SELECT * FROM tableA WHERE `idno` NOT IN (SELECT `sidno` FROM tableB)
SQL Fiddle Demo
Ok. Try this:
SELECT tableB.Id, tableB.sidno
FROM tableA
RIGHT JOIN tableB ON tableA.Id = tableB.ID
WHERE tableA.Id = 'NULL';
This should give you all the records you want.
like this
select * from tableA where Minus select id from where tableA.id=tableB.id;
MINUS
http://www.techonthenet.com/sql/minus.php
Try this
SELECT * FROM table2 t where sid NOT IN (select id from table1) ;
Demo
I'm trying to create a list in PHP of the oldest entries for each user in the database.
SELECT *,
MIN(`entries`.`entry_date`)
AS entry_date
FROM (`entries`)
JOIN `user_profiles`
ON `user_profiles`.`user_id` = `entries`.`user_id`
WHERE `status` = 1
GROUP BY `entries`.`user_id`
I'm using the query to retrieve from the entries table the oldest dated entry using MIN()and joining with table user_profiles for other data. The query should select the oldest entry for each user. It seems to work but it retrieves the wrong entry_date field on some entries when I echo them. Please help, I can't spot what I'm doing wrong..
You need to use a subquery to obtain the (user_id, entry_date) pairs for each user, then join that with a query that selects the records of interest:
SELECT *
FROM entries
NATURAL JOIN (
SELECT user_id, MIN(entry_date) AS entry_date
FROM entries
GROUP BY user_id
) AS tmin
JOIN user_profiles USING (user_id)
WHERE status = 1
Have you tried approaching the problem from the user_profiles table instead of the entries table? If a user has no entries, they will not appear in the above query.
This may help, but I'm not sure if it's the full solution:
SELECT *, MIN(entries.entry_date) as entry_date
FROM user_profiles LEFT JOIN entries USING (user_id)
WHERE status = 1
GROUP BY user_profiles.user_id
Also, you're renaming the MIN(entires.entry_date) as entry_date... but you already have a column named entry_date. Try renaming the derived columns to something unique like "min_entry_date"?
I have a table of data which has posts, then I have a separate table of data which has deleted posts. What happens when a post is deleted is that it's ID get's added to the deleted table rather than removing the post entry.
What is a clean efficient way of selecting all the posts from the posts table without selecting the ones that have their ID in the deleted table
If you can't change your tables, you can do a Left Join (which will join your deleted_table when possible) and then check for the id to be Null (means that no row has been found).
Select everything_you_need
From post_table p
Left Join delete_table d On ( d.id = p.id )
Where d.id Is Null;
Make sure to have indexes on both id-columns for best performance.
Would it not be easier to add an extra column to your posts table and store and boolean value of deleted? Or use an integer field (to represent user id) if you want to store who deleted it, 0 = not deleted.
If you have a good reason not to use a boolean flag as bigstylee suggests, you can use an EXISTS subquery:
SELECT * FROM table1
WHERE NOT EXISTS
(SELECT * FROM table2 WHERE table1.id=table2.id);