I have MySQL table where user can post some content on website as follows:
CREATE TABLE `Posts` (
`id` int(6) UNSIGNED NOT NULL,
`name` varchar(30) NOT NULL,
`post` varchar(8000) NOT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
)
Now I want to create a table for user to comment on posts. I don't know right way to do this, lack of knowledge.
Can I create new table like this
CREATE TABLE `Comments` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(30) NOT NULL,
`comment` varchar(800) NOT NULL,
`postId` varchar(8) NOT NULL,
)
and when user comments I can display that comment by connecting "id" from post table and "postID" from "comment" table.
Is this right way to do this, or is there better way to make comments on post table?
Basically, You can link with a foreign key like this.
Posts (
id int(6) UNSIGNED NOT NULL,
name varchar(30) NOT NULL,
post varchar(8000) NOT NULL,
date timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
);
You can define a foreign key for user if you want(It's a good pratice).
CREATE TABLE Comments (
id int(10) NOT NULL AUTO_INCREMENT,
name varchar(30) NOT NULL,
comment varchar(800) NOT NULL,
postId int(11) NOT NULL,
user_id int(11) DEFAULT NULL
);
ALTER TABLE Comments ADD CONSTRAINT id_fk_post
FOREIGN KEY(postId) REFERENCES Posts (id);
ALTER TABLE Comments ADD CONSTRAINT id_fk_user
FOREIGN KEY(user_id) REFERENCES Users (id);
Now, you can search all comments of post like this:
SELECT * FROM Comments where postId = 3;
CREATE TABLE `Users` (
`user_id` int(6) UNSIGNED NOT NULL,
`name` varchar(30) NOT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
)
CREATE TABLE `Comments` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(30) NOT NULL,
`comment` varchar(800) NOT NULL,
`postId` varchar(8) NOT NULL,
`user_id_fk` int(6) NOT NULL
)
So as the user posts a comment, you save the user's id in the comment table together with the comment made.
To retrieve, try something like:
$query = 'SELECT Users.user_id,
Users.name,
Comments.comment FROM User JOIN Comments ON Users.user_id= Comments.user_id_fk
';
Related
Hi i'm trying to pull the first_name of the user who was the author for a certain blog post from my database but having a bit of a problem. I'm really getting confused on how to join the two tables together and can't really find a simple explanation. Here are my two tables:
CREATE TABLE IF NOT EXISTS `goldhub_user` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(20) NOT NULL,
`username` varchar(50) NOT NULL,
`password` char(40) NOT NULL,
`first_name` varchar(20) NOT NULL,
`last_name` varchar(20) NOT NULL,
PRIMARY KEY (`user_id`),
UNIQUE KEY `goldhub_email` (`email`)
)
CREATE TABLE IF NOT EXISTS `goldhub_post` (
`post_id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`category_id` int(11) DEFAULT NULL,
`post_content` varchar(255) DEFAULT NULL,
`post_date` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`post_id`),
KEY `user_id` (`user_id`),
KEY `category_id` (`category_id`)
)
Any guidance would be much appreciated.
SELECT goldhub_user.first_name FROM goldhub_post
INNER JOIN goldhub_user ON goldhub_user.user_id = goldhub_post.user_id
WHERE goldhub_post.post_id = ?
Replace the ? with the ID of the post.
Try something like this, it would help me if you post the condition to get the author based on post
SELECT gu.first_name FROM goldhub_user gu
LEFT JOIN goldhub_post gp
ON gu.user_id = gp.user_id
WHERE gp.post_id = ?
AND gp.category_id = ?;
I have these 2 queries and i would like to join them into one but i am unsure of how to go about it.
Query 1:
$query = "SELECT * FROM ".$db_tbl_comics." WHERE ".$db_fld_comics_publisher."='".$pub_id."'
AND ".$db_fld_comics_active."='1' GROUP BY ".$db_fld_comics_arc;
Query 2:
$q2 = mysql_query('SELECT '.$db_fld_arcs_title.' FROM '.$db_tbl_arcs.'
WHERE '.$db_fld_arcs_id.'="'.$result[$db_fld_comics_arc].'"');
Comics Table:
CREATE TABLE IF NOT EXISTS `comics` (
`id` varchar(255) NOT NULL,
`arc` int(255) NOT NULL,
`title` varchar(255) NOT NULL,
`issue` decimal(5,1) DEFAULT NULL,
`price` decimal(10,2) NOT NULL,
`plot` longtext NOT NULL,
`publisher` int(255) NOT NULL,
`isbn` varchar(255) NOT NULL,
`published` date NOT NULL,
`cover` varchar(255) NOT NULL DEFAULT './images/nopic.jpg',
`added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`views` int(255) NOT NULL DEFAULT '0',
`active` int(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `arc` (`arc`,`title`,`issue`,`publisher`)
);
Arcs Table:
CREATE TABLE IF NOT EXISTS `arcs` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL,
`plot` longtext NOT NULL,
`added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `title` (`title`)
);
What I need to do is get the Arcs Title from the arcs table for the respective comic arc.
You need to use INNER JOIN for that since I presume that records are present on both tables.
SELECT a.*, b.title
FROM comics a INNER JOIN arcs b
on a.id = b.id
WHERE a.Title = 'VALUEHERE'
displays all details from comics table and the title of the arc
as simple as (joining 2 queries in one, by selecting only the required field and using IN):
SELECT
'.$db_fld_arcs_title.'
FROM '.$db_tbl_arcs.'
WHERE '.$db_fld_arcs_id.' IN (
SELECT '.$db_fld_comics_arc.'
FROM '.$db_tbl_comics.'
WHERE '.$db_fld_comics_publisher.'='".$pub_id."'
AND '.$db_fld_comics_active.'='1' GROUP BY '.$db_fld_comics_arc.'
)
This is my table layout:
-- Table structure for table `areas`
CREATE TABLE IF NOT EXISTS `areas` (
`ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`user_id` bigint(20) unsigned NOT NULL,
`country` varchar(20) NOT NULL,
`city` varchar(20) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
-- Table structure for table `matches`
CREATE TABLE IF NOT EXISTS `matches` (
`ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`user_id` bigint(20) unsigned NOT NULL,
`view_id` bigint(20) unsigned NOT NULL,
`status` enum('h','n') NOT NULL,
`exp_date` date NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
-- Table structure for table `users`
CREATE TABLE IF NOT EXISTS `users` (
`ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`limit_age` varchar(5) NOT NULL DEFAULT '18:30',
`limit_gender` varchar(2) DEFAULT NULL,
`notifications` int(11) NOT NULL DEFAULT '0',
`name` varchar(30) NOT NULL,
`email` varchar(40) NOT NULL,
`image_big` varchar(120) NOT NULL,
`image_small` varchar(120) NOT NULL,
`crop_data` int(11) DEFAULT NULL,
`visible` tinyint(1) NOT NULL DEFAULT '0',
`age` int(11) DEFAULT NULL,
`registered_at` datetime NOT NULL,
`views` bigint(20) unsigned NOT NULL DEFAULT '0',
`hots` bigint(20) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ;
I will try to explain this better:
I have a given ID.
I would like to select one entry from users which is not the ID i have given
AND which user_id does not exist in matches
AND has visible = 1
AND where any country + city matches the given users country + city
Is this the correct way to do it (12 is an example of an given ID):
SELECT *
FROM users a
INNER JOIN areas ON areas.user_id = a.id
WHERE a.id NOT IN (SELECT user_id FROM matches)
AND NOT a.id = '12'
AND a.limit_age = '18:30'
AND a.visible = '1'
AND areas.country = 'sverige'
AND areas.city = 'gbg'
Sorry for the confusion :)
Ok, I'll make an attempt at this:
SELECT *
FROM users a
INNER JOIN areas ON areas.user_id = a.id
WHERE a.id NOT IN (SELECT user_id FROM matches)
AND a.visible = '1'
AND a.limit_age = '18:30'
AND a.limit_gender = 'f'
AND areas.country = ?
AND areas.city = ?;
This is SELECTing from "users", and returning a result only if that user also has an entry in the "areas" table. The first item in the WHERE clause ensures that a row will not be returned if the users.id (a.id) is found in the user_id field on the "matches" table. Next, I added checks for visible = 1, limit_age, and limit_gender as specified in his attempt. Finally, I left country and city parameterized so that they can be added as parameters in the php code. If anything that should give you a starting point.
I'm Using 3 tables here listed below:
CREATE TABLE `user` (
`uid` int(10) NOT NULL AUTO_INCREMENT,
`kid` int(3) NOT NULL,
`Email` varchar(255) DEFAULT NULL,
`del` tinyint(1) DEFAULT '0',
PRIMARY KEY (`uid`),
KEY `kid` (`kid`),
KEY `email` (`Email`)
) ENGINE=MyISA
and
CREATE TABLE `blacklist_global` (
`bgid` int(10) NOT NULL AUTO_INCREMENT,
`email` varchar(255) NOT NULL,
`kid` int(3) DEFAULT NULL,
`stmp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`bgid`),
UNIQUE KEY `email` (`email`),
KEY `kid` (`kid`)
) ENGINE=MyISAM
and
CREATE TABLE `verteiler_user` (
`vuid` int(10) NOT NULL AUTO_INCREMENT,
`uid` int(3) NOT NULL,
`vid` int(3) NOT NULL,
`del` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`vuid`),
KEY `uid` (`uid`)
) ENGINE=MyISAM
every entry in table user is also in table verteiler_user. now i want to delete every entry from user where user.uid = verteiler_user.uid and verteiler_user.vid=XX and user.uid = XXX
so atm im doing it from php fetch all entrys from table user, all from blacklist_global.
the problem is, in the blacklist i could enter *#heloooo.de so i want to delete every email from the domain helooo.de but its very very slow.
is it possible to do it only in mysql? without php ? or any tip, of doing this faster ?
This might get what you need. Test it first before using it on production data.
DELETE FROM user WHERE uid = ? AND uid in (SELECT uid from verteiler_user WHERE vid = ?)
Edited based on comment:
DELETE FROM user WHERE uid IN (SELECT uid FROM blacklist_global WHERE email LIKE '%#somedomain.com')
I have two mysql db tables, photos and album, I would like to list photos by album how do i do that ?
CREATE TABLE `album` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`album_name` varchar(95) NOT NULL,
`album_desc` text NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `photos` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`album_id` int(11) NOT NULL,
`thumb_name` varchar(255) NOT NULL,
`photo_name` varchar(250) NOT NULL,
PRIMARY KEY (`id`)
)
Not sure about the syntax but something like this
SELECT * FROM photos GROUP BY album_id;
might do the trick.