The documentation says that the table of ratings should look like this:
CREATE TABLE taste_preferences (
user_id BIGINT NOT NULL,
item_id BIGINT NOT NULL,
preference REAL NOT NULL,
PRIMARY KEY (user_id, item_id)
);
However, in my implementation table of ratings is as follows:
CREATE TABLE taste_preferences (
profile_id BIGINT NOT NULL,
event_id BIGINT NOT NULL,
status_id BIGINT NOT NULL,
PRIMARY KEY (user_id, item_id)
);
Where the grade is in the form status_id (go, no go, maybe I'll go).
The users table as follows:
CREATE TABLE user (
profile_id_1 BIGINT NOT NULL,
profile_id_2 BIGINT NOT NULL,
profile_id_3 BIGINT NOT NULL,
...
);
A user can have multiple profiles, I need to compare these data to users.
I need to write its own implementation of data model? Which way do I see, that would solve this problem? Thanks!
You don't have rating data here, in any form. So, you can't use ratings in recommendations. That's fine; you just have "boolean" data.
Or, you're saying you need to use status but I'm not clear how you want to use it.
You can certainly use your taste_preferences table. Just use MySQLBooleanPrefJDBCDataModel or similar. The user table is irrelevant.
Related
i'm making a forum using Java technologies. Actually it is almost near to complete but the problem is I want to show numbers of answer into my forum. Okay let's understand in deeply.
Firstly, i've created a file named as index.jsp where we could see all questions. For e.g Have a look into stackoverflow.com we see all question as well numbers of answers posted in one question. That's all i wanted to show into my index.jsp.
I'm fetching all questions using select * from question_table... Actually into my view question file. I'm fetching the answer using question_id table which is created into answer's table. You know very well to show answer in particular we need to save the same question_id into answer's table as well question's table.
For e.g:
look at this table i designed the same thing:
create table if not exists thread_question(
question_id INT NOT NULL auto_increment,
question_title VARCHAR(500) NOT NULL,
question VARCHAR(100000) NOT NULL,
question_dateTime VARCHAR(100) NOT NULL,
PRIMARY KEY(question_id)
);
create table if not exists thread_answer(
answer_id INT NOT NULL auto_increment,
question_id INT NOT NULL references thread_question(question_id),
answer VARCHAR(100000) NOT NULL,
answer_dateTime VARCHAR(100) NOT NULL,
PRIMARY KEY(answer_id)
);
As well you could see here, question_id is saved into two tables. Same process i'm using here.
Now i want to show my number of answers into my main page. Any idea? what can be used here. I really stuck here. Please help!
Surely, Help would be appreciated!!
EDITED:
Here is my full codes of table:
create table if not exists thread_question(
question_id INT NOT NULL auto_increment,
first_name VARCHAR(100) NOT NULL,
last_name VARCHAR(100) NOT NULL,
question_title VARCHAR(500) NOT NULL,
question VARCHAR(100000) NOT NULL,
question_dateTime VARCHAR(100) NOT NULL,
PRIMARY KEY(question_id)
);
create table if not exists thread_answer(
answer_id INT NOT NULL auto_increment,
question_id INT NOT NULL references thread_question(question_id),
first_name VARCHAR(100) NOT NULL,
last_name VARCHAR(100) NOT NULL,
answer VARCHAR(100000) NOT NULL,
answer_dateTime VARCHAR(100) NOT NULL,
PRIMARY KEY(answer_id)
);
Here is SCREENSHOT.
PLEASE HELP!!
use inner join to fetch number of answer per question
SELECT thread_question.question_id, COALESCE(sub.counts,0) AS NumerOfAnswer
FROM thread_question LEFT JOIN (
SELECT question_id, COUNT(answer_id) AS counts
FROM thread_answer
GROUP BY question_id
) sub ON thread_question.question_id = sub.question_id
ORDER BY NumerOfAnswer
The count of all answers in the entire forum?
SELECT COUNT(answer_id)
FROM thread_answer
Display number of answers in a query that fetches questions for display?
SELECT <...your selected columns...>, COALESCE(a.counts, 0) AS counts
FROM thread_question q
LEFT JOIN (
SELECT question_id, COUNT(answer_id) AS counts
FROM thread_answers
GROUP BY question_id
) a ON q.question_id = a.question_id
I'm making a forum and now I've this table. Please take a look at here:
This table is for users:
create table if not exists login_system(
user_id INT NOT NULL auto_increment,
email VARCHAR(100) NOT NULL,
password VARCHAR(100) NOT NULL,
first_name VARCHAR(100) NOT NULL,
last_name VARCHAR(100) NOT NULL,
gender VARCHAR(50) NOT NULL,
about_yourself VARCHAR(150) NOT NULL,
my_website VARCHAR(100000) NOT NULL,
PRIMARY KEY(user_id)
);
And this table is for questions:
create table if not exists thread_question(
question_id INT NOT NULL auto_increment,
first_name VARCHAR(100) NOT NULL,
last_name VARCHAR(100) NOT NULL,
question_title VARCHAR(500) NOT NULL,
question VARCHAR(100000) NOT NULL,
question_dateTime VARCHAR(100) NOT NULL,
PRIMARY KEY(question_id)
);
I want to show a user's link If he/she posts his/her question. It must suppose to provide a link after hover on name of the persons. So that other peoples could see his/her profile information.
I can simply fetch some questions to show into forum using this:
<%
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/forum", "root", "1234");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from thread_question where question_id="+question_id);
while(rs.next()){
String first_name=rs.getString("first_name");
String last_name=rs.getString("last_name");
String thread_title=rs.getString("question_title");
String thread_question=rs.getString("question");
String thread_dateTime=rs.getString("question_dateTime");
...
...
...
%>
Now my wish is to show login_system's table along with question's table you know pretty well that we couldn't create two statements along here. So please help.. I want to get user_id from login_system table where i could easily show his/her profile by clicking the link. Can you please help me? How to do that?
First follow a horse_with_no_name's adviece an do NOT put SQL code into your JSP pages for security reasons.
Also please fix your grammar/spelling as it was really hard to read your question.
Another thing is you are going to want to go read about normalizing database structures http://en.wikipedia.org/wiki/Database_normalization
I know pretty well that you can make more than one sql statement if you would like to, it's just another connection, but in this case you should not need to (And most cases)
Change your thread_question table to this
create table if not exists thread_question(
question_id INT NOT NULL auto_increment,
user_id id INT NOT NULL,
first_name VARCHAR(100) NOT NULL,
last_name VARCHAR(100) NOT NULL,
question_title VARCHAR(500) NOT NULL,
question VARCHAR(100000) NOT NULL,
question_dateTime VARCHAR(100) NOT NULL,
PRIMARY KEY(question_id)
);
It looks like the SQL you want is something like this?
SELECT
LS.FIRST_NAME,
LS.LAST_NAME,
TQ.question_title,
TQ.question,
TQ.question_dateTime
FROM
login_system as LS
JOIN thread_question as TQ ON
LS.user_id = TQ.user_id
There are various issues with your approach. First, as commented, you should not put the connection code directly into the JSP. Second, you should not create the statement by concatenating strings because of the risk of SQL injection. Make sure you understand what that means and use a PreparedStatement instead.
To finally answer your question, rather than having first and last name in the thread_question table, you should have a foreign key to the id in the login_system table and then use a join in your SQL statement. I guess it would not help you a lot if I would paste the verbatim solution here.
Follow Shifty solution and if you don't want to join the table then there is one more solution i.e
1. Fire query to you your thread_question and take data.
2. When you are preparing your page and giving userLink you have to attach hover event in javascript which take userid as input and fire ajax call to take the user related data and use this data.
I was trying to create a new object and this error appeared:
java.sql.sqlexception failed to read auto-increment value from storage engine
So I went to the phpMyAdmin to create the object there and the same showed up:
MySQL said: Documentation
1467 - Failed to read auto-increment value from storage engine
then I clicked on edit, and it was there:
INSERT INTO `reservation`.`room` (`idroom`, `number`, `floor`, `description`, `characteristics`, `cost`, `status`, `type`) VALUES (NULL, '114', '3', 'ss', 'ss', '550.00', 'Available', 'ss')
(idroom is supposed to be auto-incremented.)
I already read other posts where they say I have to put this:
ALTER TABLE `table_name` AUTO_INCREMENT = 1
but I have no idea where to put that. Is there a better solution?
Your INSERT statement is wrong. Since idroom is AUTO_INCREMENT; you must not include it in the column list on your insert command. Your insert statement should look like below. Notice that I have removed idroom column from insert column list and not passing NULL as well in value list.
INSERT INTO `reservation`.`room` (`number`, `floor`, `description`,
`characteristics`, `cost`, `status`, `type`)
VALUES ('114', '3', 'ss', 'ss', '550.00', 'Available', 'ss')
I also struggled with this problem and searched, and didn't find anything. Then the following worked for me; I guess it might work for your problem. Thx.
1st:
-delete (before backup)->all data from your database.
-try to run your Java program again, or any program you want.
If it fails then go to 2nd.
2nd:
- backup all data from your table
- delete table completely
- create table again; example shown below:
CREATE TABLE `users` (
`id` int(6) NOT NULL,
`f_name` varchar(30) NOT NULL,
`l_name` varchar(30) NOT NULL,
`address` varchar(50) DEFAULT NULL,
`phone_num` varchar(12) DEFAULT NULL,
`email` varchar(30) DEFAULT NULL
);
ALTER TABLE `users`
ADD PRIMARY KEY (`id`);
AUTO_INCREMENT for table `users`
ALTER TABLE `users`
MODIFY `id` int(6) NOT NULL AUTO_INCREMENT;
I have 2 tables. First one holds the total values of some shopping lists and the second table holds the products in that list. When a shopping list is done the total value is added into the total table together with some informations like the list number(nrList which is some kind of list id) and the number of products on that list nrProducts while the products go into the listproducts table.Lets say there are 3 products tomato,oranges and apples.They will all share the same nrList which,as mentioned before,is something like the list id.
First table totals:
CREATE TABLE IF NOT EXISTS `totals` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`nrList` int(11) NOT NULL,
`nrProducts` int(11) DEFAULT NULL,
`total` double NOT NULL,
`data` date DEFAULT NULL,
`ora` time DEFAULT NULL,
`dataora` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`Operator` varchar(50) DEFAULT NULL,
`anulat` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
)
Second table listproducts:
CREATE TABLE IF NOT EXISTS `listproducts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`nrList` int(11) DEFAULT NULL,
`product` varchar(50) DEFAULT NULL,
`quantity` double DEFAULT NULL,
`price` double DEFAULT NULL,
`data` date DEFAULT NULL,
`operator` varchar(50) NOT NULL,
`anulat` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
)
Now,i have two things i want to do,they are very similar.
Lets say i have a list with 3 products.In the totals table there will be a row with some info and with the total=10$ nrProducts=3 and nrList=1.In the listproducts table i will have 3 rows all having nrList=1 and each having price=3$,3$,4$.
Now,i want the check the following :
1.That if the value of nrProducts=3 then i have products for that list in the other table.
2.Check if the total in the first table is equal to the sum of the products in the second table.(quantity*price SUM)
I've done some stuff but i don't know what to do next.
I managed to get the number of products for each list from the second table by using this:
SELECT nrList,operator,COUNT(*) as count FROM listproducts GROUP BY nrList
But i don't know how to compare if the values are equal without doing two queries.
For the second thing again, I know how to get the sum but i don't know how to compare them without doing two separate queries.
SELECT SUM(price*quantity) FROM `listproducts` WHERE nrList='10' and operator like '%x%'
I can also do something like what i've done in the other select,this is not the issue.
The issue is that i don't know how to do the things i want in a single select instead of doing two and comparing them.I'm doing this in java so i can compare but i'd like to know if and how i can do this in a single query.
Thanks and sorry for the long post.
You can try something like this:
SELECT totals.nrList,
IF (totals.nrProducts = t.nrProductsActual, 'yes', 'no') AS matchNrProducts,
IF (totals.total = t.totalActual, 'yes', 'no') AS matchTotal
FROM totals INNER JOIN
(SELECT nrList,
COUNT(*) AS nrProductsActual,
SUM(quantity*price) AS totalActual
FROM listproducts
GROUP BY nrList) AS t ON totals.nrList = t.nrList
I want to design a page tracker database table, but I am facing few issues with it.
create table pageTracker(
ID bigint(20) NOT NULL,
TrackerID bigint(20) NOT NULL,
SessionID varchar(100) NOT NULL,
pageViews bigint,
pageVisits bigint,
primary key(ID)
);
If I update pageviews and pageVisits corresponding to specific SessionID I can not query pageViews and pageVisits within specific time interval.
create table pageTracker(
ID bigint(20) NOT NULL,
TrackerID bigint(20) NOT NULL,
SessionID varchar(100) NOT NULL,
pageViews bigint,
pageVisits bigint,
time TimeStamp,
primary key(ID)
);
But if I add extra column time, if I want to insert each pageViews and pageVisits as new entry for specific time it creates huge number of entry in the table.
Is there any efficient way to do it?
I am assuming that you want to update pageViews and pageVisits everytime against a SessionID. In this case first insert will have say:
Session ID = 23R4E11, pageViews = 1, pageVisits = 1
Now if same user revisits same page, you will update existing row as:
Session ID = 23R4E11, pageViews = 2, pageVisits = 1
In this case to maintain all the updates, you can create one more table called as pageTrackerHistory and then write trigger which can insert entry in pageTrackerHistory table whenever update is made on pageTracker table.
By doing this your operational table pageTracker contains minimal rows and pageTrackerHistory table contains huge audit records.
Hope this will give you some direction. :-)