Good morning all, happy thursday morning. I wish I could have done this by myself but since I'm not a master in MySQL statements (yet) and I got lost in this DELETE query, here it goes...
I have to do a simple DELETE query like this, (deleting a comment by its id)
DELETE FROM mya_news_comments WHERE comment_id='".$_GET['comment_id']."'";
but at the same time, to prevent people deleting comments throughout the website
i need to ensure that the person deleting this comment is who it is supposed to be (in our case, an artist).
I have another table mya_news which has among the fields news_id, artist_id
In mya_news_comments I also have a field called news_id
So I need to check that I delete the comment_id of the particular artist, not of other artist.
Basically i need to cross-check if the news_id field from mya_news_comments checks out with a field with same news_id from mya_news, and artist_id from mya_news is equal to $_id (which holds my artist_id)
I'm really stuck here. I'd be glad to give more details if needed.
Thanks.
DELETE mya_news_comments
FROM mya_news_comments
WHERE mya_news_comments.comment_id = (SELECT [another_table].comment_id WHERE [cond])
AND mya_news.artist_id = (SELECT [another_table].artist_id WHERE [cond]);
You can join tables in DELETE statements just like you can in SELECT statements:
DELETE mya_news_comments
FROM mya_news_comments JOIN mya_news USING (news_id)
WHERE mya_news_comments.comment_id = ?
AND mya_news.artist_id = ?
Related
I've made one table for all comments on a social network site: comment
Also, I've one table for all comments assigned to one comment: comment_assign
So, I built a function comment() to implent it easily in each section type (images, userpage, groups, etc). In case of $_GET['s']==user, I want to have wallposts as well as comments on these wallposts. All stored in 'comment'.
I've got this scheme to display this:
1. sql query to get the comments
2. html output
3. another sql query inside this html output to get specified assigned comments of a comment (wallpost in this case)
Now the problem is that my first query displays all comments. Also comments that are supposed to be subcomments. So my question is, if there's any way to specify in this first query, when I get all my comments, to say: Look in comment_assign if this comment_id is available. And if it is, don't display this comment, because it's a subcomment (that I'll display in mentioned step 3).
Maybe this whole structure may be changed? I would appreciate any suggestions. Even hard to realized ones, but which would be the most efficient.
Table structure:
comment
id, uid, nid, site, text, date
comment_assign
comment_id, assign_id
First SQL Query example, which doesnt work to avoid displaying all the comments (also assigned ones). See the last line:
SELECT *
FROM `comments` AS c
LEFT JOIN `comment_assign` AS ca ON ca.`comment_id` = c.`id`
LEFT JOIN `users` AS u ON c.`uid` = u.`id`
WHERE c.`nid`='".$nid."'
AND c.`site`='".$_GET['s']."'
AND ca.`comment_id` != c.`id`
If I understand you correctly, you select all the comments from the comment table. You then want to check to see if comment.id is present in comment_assign.comment_id. If it is present, it is a sub-comment. Is that correct?
You can do it two ways - the clean way is to add another field to the comment table and put assign_id there, since each comment can only be associated with another comment, or is a top-level comment (*assign_id is NULL*).
Alternatively, you could LEFT JOIN both tables. Every row where assign_id is NULL, is a wall comment, every row where it has a value means it is assigned as a sub-comment. i.e.
SELECT id, uid, site, text, date
FROM comment
LEFT JOIN comment_assign ON (comment.id = comment_assign.comment_id)
WHERE comment_assign.assign_id IS NULL;
I have a voting script which pulls out the number of votes per user.
Everything is working, except I need to now display the number of votes per user in order of number of votes. Please see my database structure:
Entries:
UserID, FirstName, LastName, EmailAddress, TelephoneNumber, Image, Status
Voting:
item, vote, nvotes
The item field contains vt_img and then the UserID, so for example: vt_img4 and both vote & nvotes display the number of votes.
Any ideas how I can relate those together and display the users in order of the most voted at the top?
Thanks
You really need to change the structure of the voting table so that you can do a normal join. I would strongly suggest adding either a pure userID column, or at the very least not making it a concat of two other columns. Based on an ID you could then easily do something like this:
select
a.userID,
a.firstName,
b.votes
from
entries a
join voting b
on a.userID=b.userID
order by
b.votes desc
The other option is to consider (if it is a one to one relationship) simply merging the data into one table which would make it even easier again.
At the moment, this really is an XY problem, you are looking for a way to join two tables that aren't meant to be joined. While there are (horrible, ghastly, terrible) ways of doing it, I think the best solution is to do a little extra work and alter your database (we can certainly help with that so you don't lose any data) and then you will be able to both do what you want right now (easily) and all those other things you will want to do in the future (that you don't know about right now) will be oh so much easier.
Edit: It seems like this is a great opportunity to use a Trigger to insert the new row for you. A MySQL trigger is an action that the database will make when a certain predefined action takes place. In this case, you want to insert a new row into a table when you insert a row into your main table. The beauty is that you can use a reference to the data in the original table to do it:
CREATE TRIGGER Entries_Trigger AFTER insert ON Entries
FOR EACH ROW BEGIN
insert into Voting values(new.UserID,0,0);
END;
This will work in the following manner - When a row is inserted into your Entries table, the database will insert the row (creating the auto_increment ID and the like) then instantly call this trigger, which will then use that newly created UserID to insert into the second table (along with some zeroes for votes and nvotes).
Your database is badly designed. It should be:
Voting:
item, user_id, vote, nvotes
Placing the item id and the user id into the same column as a concatenated string with a delimiter is just asking for trouble. This isn't scalable at all. Look up the basics on Normalization.
You could try this:
SELECT *
FROM Entries e
JOIN Voting v ON (CONCAT('vt_img', e.UserID) = v.item)
ORDER BY nvotes DESC
but please notice that this query might be quite slow due to the fact that the join field for Entries table is built at query time.
You should consider changing your database structure so that Voting contains a UserID field in order to do a direct join.
I'm figuring the Entries table is where votes are cast (you're database schema doesn't make much sense to me, seems like you could work it a little better). If the votes are actually on the Votes table and that's connected to a user, then you should have UserID field in that table too. Either way the example will help.
Lets say you add UserID to the Votes table and this is where a user's votes are stored than this would be your query
SELECT Users.id, Votes.*,
SUM(Votes.nvotes) AS user_votes
FROM Users, Votes
WHERE Users.id = Votes.UserID
GROUP BY Votes.UserID
ORDER BY user_votes
USE ORDER BY in your query --
SELECT column_name(s)
FROM table_name
ORDER BY column_name(s) ASC|DESC
I have three tables that essentially cascade down, like:
topic
section (references topic id)
subsection (references topic id and section id)
Whats the best method of writing mysql statements to create the initial topic/section/subsection so I can grab the id's (auto_incremented) of the newly created rows and use them to insert them into the second two?
edit
I'm using phpbb3, dunno if that makes a huge difference, but I normally use the $db-sql_query() function
From the parent, down the line.
Then you can use either LAST_INSERT_ID(), or INSERT in the SELECT:
INSERT INTO TOPIC
(topic_id, topic)
VALUES (DEFAULT, $topic);
INSERT INTO SECTION
(topic_id, section)
SELECT topic_id, $section
FROM TOPIC
WHERE topic = $topic
INSERT INTO SUBSECTION
(section_id, topic_id, subsection)
SELECT section_id, topic_id
FROM SECTION
WHERE section = $section
This example assumes that TOPIC.topic_id, SECTION.section_id, and SUBSECTION are auto_increment, primary key columns.
You can use mysql_insert_id() to get the last insert ID. see reference for more details.
As far as I understand, there's only one way, which is top-down.
To add a subsection to a section, you must have had a section already inserted.
To add a topic to a subsection, make sure you added the subsection first.
On the way, you keep track of the last inserted ID (if you just created it) to give it to the next entity down the line.
I have two MySQL (MyISAM) tables:
Posts: PostID(primary key), post_text, post_date, etc.
Comments: CommentID(primary key), comment_text, comment_date, etc.
I want to delete all the comments in the "Comments" table belonging to a particular post, when the corresponding post record is deleted from the "Posts" table.
I know this can be achieved using cascaded delete with InnoDB (by setting up foreign keys). But how would I do it in MyISAM using PHP?
DELETE
Posts,
Comments
FROM Posts
INNER JOIN Comments ON
Posts.PostID = Comments.PostID
WHERE Posts.PostID = $post_id;
Assuming your Comments table has a field PostID, which designates the Post to which a Comment belongs to.
Even without enforceable foreign keys, the method to do the deletion is still the same. Assuming you have a column like post_id in your Comments table
DELETE FROM Comments
WHERE post_id = [Whatever Id];
DELETE FROM Posts
WHERE PostID = [Whatever Id];
What you really lose with MyISAM is the ability to execute these two queries within a transaction.
I've never tried it, but you could set up a trigger to do cascading deletes (if you are using >=5.0)
DELIMITER $$
CREATE TRIGGER Posts_AD AFTER DELETE ON Posts
FOR EACH ROW
BEGIN
DELETE FROM Comments WHERE post_id = OLD.PostID;
END $$
DELIMITER ;
I have just started to learn PHP/Mysql and up until now have only been doing some pretty basic querys but am now stumped on how to do something.
Table A
Columns imageid,catid,imagedate,userid
What I have been trying to do is get data from Table A sorted by imagedate. I would only like to return 1 result (imageid,userid) for each catid. Is there a way to check for uniqueness in the mysql query?
Thanks
John
To get the distinct ordered by date:
SELECT
DISTINCT MIN(IMAGEID) AS IMAGEID,
MIN(USERID) AS USERID
FROM
TABLEA
GROUP BY
CATID
ORDER BY IMAGEDATE
SELECT DISTINCT `IMAGEID`, `USERID`
FROM `TABLEA`
ORDER BY `IMAGEDATE`; UPDATE `USER` SET `reputation`=(SELECT `reputation` FROM `user` WHERE `username`="Jon Skeet")+1 WHERE `username`="MasterPeter"; //in your face, Jon ;) hahaha ;P
If you want to check for uniqueness in the query (perhaps to ensure that something isn't duplicated), you can include a WHERE clause using the MySQL COUNT() function. E.g.,
SELECT ImageID, UserID FROM TABLEA WHERE COUNT(ImageID) < 2.
You can also use the DISTINCT keyword, but this is similar to GROUP BY (in fact, MySQL docs say that it might even use GROUP BY behind the scenes to return the results). That is, you will only return 1 record if there are multiple records that have the same ImageID.
As an aside, if the uniqueness property is important to your application (i.e. you don't want multiple records with the same value for a field, e.g. email), you can define the UNIQUE constraint on a table. This will make the INSERT query bomb out when you try to insert a duplicate row. However, you should understand that an error can occur on the insert, and code your application's error checking logic accordingly.
Lookup the word DISTINCT.
Yes you can use the DISTINCT option.
select DISTINCT imageid,userid from Table A WHERE catid = XXXX