Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Insert a value in a column name e-mail then it display all emails seperated by comma. This email is save at different time but for same id.
Is it possible then how?
You can use GROUP_CONCAT to show the comma separated records that belong to same group,be aware of that fact GROUP_CONCAT has a default limit of 1024 characters to concat but it can be increased as mentioned in docs
CREATE TABLE Table1
(id INT ,`test` varchar(25))
;
INSERT INTO Table1
(id,`test`)
VALUES
(1,'test#test.com'),
(1,'test#test.com'),
(1,'test#test.com'),
(2,'test2#test.com'),
(2,'test2#test.com'),
(2,'test2#test.com')
;
SELECT id, GROUP_CONCAT(test) emails
FROM
Table1 GROUP BY id
Fiddle Demo
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
A MySQL table called SQ was created as follows,
CREATE TABLE SQ(
SQno INT PRIMARY KEY,
Question VARCHAR(100)
);
After I was trying to enter data to that table like this,
INSERT INTO SQ (SQno, Question) VALUES(
(1,'What primary school did you attend?'),
(2,'In what town or city did your parents meet?'),
(3,'In what city or town was your first job?'),
);
But there was an error occurs, and shows as this,
ERROR 1136 (21S01): Column count doesn't match value count at row 1
So, please help me to resolve this error?
You need to get rid of the outer set of parentheses; each row should have parentheses around it but not also all the rows:
INSERT INTO SQ (SQno, Question) VALUES
(1,'What primary school did you attend?'),
(2,'In what town or city did your parents meet?'),
(3,'In what city or town was your first job?')
;
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have a mysql database and some tables.
One table is Products: Its columns are prod_id, prod_type.
Prod_types are mobile or laptop or book.
Each of them have different attributes (eg: publisher, author for books, battery_condition, model for laptop etc).
Should I store all the data in the Products table by adding columns like 'author', 'model' etc and keep null for those columns which don't apply to my product.
For example for an entry with prod_type='book' , 'model', 'batter_condition' etc will be null. Or should I create different tables for 'book', 'mobile' and 'laptop' ?
Yes. Create new tables for Book, Mobile and Laptop. Have the product_id attribute in each of those tables. This product_id attribute is a foreign key to the primary key of table products, which is product_id as well.
Storing all data in products will result in a lot of NULL fields and will make the data storage inefficient.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a table with two attributes
Staffnumber Name
Now I want to add a new staff member into this table, for example: Christian, and Staffnumber is the current max Staffnumber + 1. (Assume I have til now 20 staff members, then Christian would be number 21)
How could i do that, without knowing how many staff members I had? Is there any way to make SQL numbers the Christian as 21?
Making a call to the table to count the records should achieve sort of what you're looking for. Forgive my syntax but it should be something along these lines...
INSERT INTO TABLE_NAME (STAFF_NUMBER, NAME) VALUES ((SELECT COUNT(*) FROM TABLE_NAME) + 1, "Christian");
Keep in mind if you delete staff members and add more later this won't work because the count will be off.
If you make your STAFF_NUMBER column a primary key, you shouldn't have to specify this value at all when entering new values, it should auto-increment and assign a random ID to the staff member.
INSERT INTO TABLE_NAME (NAME) VALUES ("Christian");
If there is no auto-increment on your column, then you would just do a simple INSERT statement:
INSERT INTO table_name (Staffnumber, Name)
VALUES (21, 'Christian')
Also here is a link to set AUTO_INCREMENT for your column for MySQL:
http://www.w3schools.com/sql/sql_autoincrement.asp
Increment
So if you don't have auto increment. You will want to select the COUNT() from Staffnumber.
SELECT COUNT(*) + 1 From Table;
Inserting
Now you also want to insert a new record into the table.
INSERT INTO Customers VALUES(1,"Christian");
But the problem with this is that it will only Staffnumber of 1. So you need to combine both of these.
Final
INSERT INTO Table VALUES((SELECT COUNT(*)+1 FROM Table),"Christian");
INSERT INTO Table VALUES((SELECT COUNT(*) FROM Table) + 1,"Christian");
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Let's say i have two tables.
Table client
ID - primary key ( 1,2,3,4,5,6)
Table Orders
OrderID - primary key
ClientID (1,2,4,5)
I need to get ROWS of table CLIENT of clients 3 and 6 (which don't have orders / are not in orders table)
This is basic query. Try your self.
select * from client where id not in (select ClientID from orders)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How can I change the column position to first using ALTER query in MySQL table?
If you want your column at first position, you should use
ALTER TABLE tableName MODIFY COLUMN yourColumnName varchar(50) FIRST
For example, if your table is
Employee{FirstName,LastName,EmpId}
And if you want to move EmpId to first position
ALTER TABLE Employee MODIFY COLUMN EmpId varchar(15) FIRST;
For example :
Below query will bring empname column after department :
ALTER TABLE Employees MODIFY COLUMN empName VARCHAR(50) AFTER department;