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.
Related
I followed this complete beginners tutorial:
http://www.homeandlearn.co.uk/java/databases_and_java_forms.html and now have a question:
I want to have a field that only can hold a limited number of predefined values?
You can use a CHECK Constraint:
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
DegreeLevel varchar(30) CHECK (DegreeLevel IN ('Msc.', 'Phd.', 'Without studies'))
);
Take a look:
https://www.w3schools.com/sql/sql_check.asp
I've this table, Have a look there.
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)
);
Actually i'm making a forum to show answers, it is working perfect. You could see as well question_id's column is inserted in thread_question as well thread_answer. I wanted to show my answers in one page that how many users has posted there answer.
So i could do that according to this question -> How to show the numbers of answers posted into my forum?
Question
Now I'm trying to combine these two statements:
First statement :
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 asc
Second statement :
select * from thread_question
Actually i want to fetch user's first and second name, question and question's title from thread_question's table.
I do use UNION and UNION ALL and SELECT( SELECT..)(SELECT..). But i'm unable to show the result. It's giving me an error everytime.
PLEASE HELP!!
Surely, help would be appreciated!!
Since you already have thread_question table in the query, all you need to do to bring the remaining columns is to add thread_question.*, like this:
SELECT
thread_question.* -- <<== Use .* to bring all fields
, 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 asc
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 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;
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.