CREATE TABLE IF NOT EXISTS `test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`form_id` tinyint(4) NOT NULL COMMENT '1=shortfor;2=longfom',
`remote_addr` varchar(19) NOT NULL,
`type` tinyint(4) NOT NULL COMMENT '1=impression;2=click;3=conversion',
`website_url` varchar(50) NOT NULL,
`c_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `form_id` (`form_id` , `remote_addr` , `type` , `website_url`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=64;
i want to total count impression,click,conversion.
i want group by website url group by impression,group by click ,group by conversion.
how to wright query? please help me i need to emergency !!!!!
![enter image description here][1]
I need following result
website url----------Impression----click--converison-----
192.1.1.1--------------1-----------2------1--------------
192.1.1.2--------------3-----------2------2--------------
192.1.1.3--------------4-----------3------1--------------
192.1.1.4--------------2-----------6------1--------------
please try below query
$sql = "SELECT website_url,SUM(type=1) as impression,SUM(type=2) as click,SUM(type=3) as conversion FROM `test` GROUP BY website_url";
Let me know this is the way you need.
Related
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
';
I have two tables in my mysql database called forum_topics and forum_replies.
I am looking to have an overview of the latest posts / replies added, like this: http://prntscr.com/6ixtz4
to do so, i need a way to makee it sort by the time in both tables, and if a forum_reply is in the result it need to get its topic from the forum_topics.
How can i make this work? I have no mysql query to referer to to make this work.
CREATE TABLE IF NOT EXISTS `forum_topics` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`topic` varchar(255) NOT NULL,
`thread` varchar(255) NOT NULL,
`level` int(2) NOT NULL DEFAULT '0',
`creator` int(255) NOT NULL,
`time` varchar(255) NOT NULL,
`innlegg` text NOT NULL,
`ip` varchar(255) NOT NULL,
`locked` enum('yes','no') NOT NULL DEFAULT 'no',
`deleted` enum('yes','no') NOT NULL DEFAULT 'no',
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
CREATE TABLE IF NOT EXISTS `forum_replies` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`topicid` int(255) NOT NULL,
`userid` int(255) NOT NULL,
`time` int(255) NOT NULL,
`reply` text NOT NULL,
`deleted` enum('yes','no') NOT NULL DEFAULT 'no',
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=20 ;
exampledata:
in table forum_topics
id |topic| time|
1 | test | 10
2 | test2 | 29
in table forum replies:
id | topicid | time |
1 | 1 | 18
2 | 2 | 28
As in this example i would like the outcome to be sorted something like this:
10
18
28
29
if forum_replies is one of the results it would need some data from forum_topics, but if forum_topics is one of the results it wouldnt need any data from the forum_replies.
The topicid is the id from the forum_topics of the topic that the reply is a response to.
So in the end i want to create the example output as in the image.
EDIT:
it would be a result containing in following order:
10
18
28
29
By loading the data in the example data sets, this will get the time from both tables, but sort the time from both as if they were in one table. The below query gets the results, and then orders them in order of time. Since both tables have the same structure, you can "union" them together as if they were one table and sort by time using "order_by"
select time
from forum_topics
UNION
select time
from forum_replies
order by time
Note: I was able to load both tables in the above request but without the last line on each. In other words I trimmed the part referring to the engine, and then just created the tables, and then entered the data provided..
I'm sorry I have to say the table structure of a forum post is incorrect. In your case, you will be very hard to sort a post based on topic and reply. I would suggest a table structure like this:
CREATE TABLE IF NOT EXISTS `forum_topics` (
`tid` int(255) NOT NULL AUTO_INCREMENT, // Topic ID
`tsubject` varchar(255) NOT NULL, // Topic Subject
`level` int(2) NOT NULL DEFAULT '0',
`creator` int(255) NOT NULL,
`innlegg` text NOT NULL, // I don't know what this means ...
`locked` enum('yes','no') NOT NULL DEFAULT 'no',
`deleted` enum('yes','no') NOT NULL DEFAULT 'no',
PRIMARY KEY (`tid`),
UNIQUE KEY `id` (`tid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5;
CREATE TABLE IF NOT EXISTS `forum_posts` (
`pid` int(255) NOT NULL AUTO_INCREMENT,
`tid` int(255) NOT NULL,
`userid` int(255) NOT NULL,
`time` int(255) NOT NULL,
`pcontent` text NOT NULL, // Post Content
`isauthorpost` int(1) NOT NULL, // Optional: If this is the first post of the topic
`deleted` enum('yes','no') NOT NULL DEFAULT 'no',
PRIMARY KEY (`pid`),
UNIQUE KEY `id` (`pid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=20 ;
So now you will have a topic id to follow by the first. And you can just search for all replies with ORDER BY pid, for the topic details, you can use LEFT JOIN forum_topics or INNER JOIN to finish.
Im using phpmyadmin and I have a table categories with 4 fields:
-> id
-> id_father_category (I put this with predefined: NULL) and I also put a checkbox Null
-> name
-> content
I want to give null values to my id_father_category field.
But Im having this error:
Warning: #1366 Incorrect integer value: '' for column 'id_father_category field' at row 1
And my field id_father_category stays automatically with value 0, and I dont want that.
Somebody there knows how I can solve this problem?
CREATE TABLE IF NOT EXISTS `categories` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_father_category` int(11) DEFAULT NULL,
`name` varchar(64) NOT NULL,
`content` varchar(256) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Hope this will solve your problem...
This is an addition to #Nirjhor's answer.
ALTER TABLE categories MODIFY id_father_category INT(11) DEFAULT NULL;
or you can recreate the whole thing:
CREATE TABLE IF NOT EXISTS `categories` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_father_category` int(11) DEFAULT NULL,
`name` varchar(64) NOT NULL,
`content` varchar(256) NOT NULL,
PRIMARY KEY (`id`) )
ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Setting the default value to NULL will handle your issue as well as set the field to NULL. One thing to note here is to make sure you don't have some weird index on that field cause that can also create an issue.
I have made some Vote script, where user vote up or down for some answers. It works fine when i have only one answer for question. If i give two or more answers for One question, the i got an error subquery returns more than one row.
$rezultati = mysqli_query($con,"SELECT SUM(down) as down
FROM vote WHERE answerId =
(SELECT questionId FROM answers WHERE questionId = $id)");
while($row = mysqli_fetch_array($rezultati))
{
echo $row['down'];
}
mysqli_close($con);
?>
This is for UP button, and for Down is similar. (I know that there is simplier way to make all that, but i am new at php..)
Here is table questions
CREATE TABLE IF NOT EXISTS `questions` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`question` text NOT NULL,
`user` varchar(255) NOT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
And table Answers
CREATE TABLE IF NOT EXISTS `answers` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`answer` text COLLATE utf8_unicode_ci NOT NULL,
`questionId` int(11) NOT NULL,
`user` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
)
And table Vote
CREATE TABLE IF NOT EXISTS `vote` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`ip` varchar(50) NOT NULL,
`answerId` int(11) NOT NULL,
`up` int(11) NOT NULL,
`down` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;
Where is the problem? How to fix that? Thank you!!
From your question I understand that you want to list all down votes for a certain question. Then, if you are updating the columns up and down to table answers, the query is simple.
"SELECT SUM(down) as down
FROM answers WHERE questionId = $id)"
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')