Why doesnt this insert statement work? - mysql

I tried this insert statement in mysql ( phpmyadmin to be exact but I think its a mysql syntax error )
INSERT INTO rented_video
VALUES(NULL, 1, 2),
VALUES(NULL, 2, 1),
VALUES(NULL, 2, 2),
VALUES(NULL, 2, 3),
VALUES(NULL, 3, 4),
VALUES(NULL, 3, 5),
VALUES(NULL, 4, 6),
VALUES(NULL, 4, 7),
VALUES(NULL, 4, 8),
VALUES(NULL, 5, 9);
The Error is :
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
near 'VALUES(NULL, 2, 1), VALUES(NULL, 2, 2), VALUES(NULL, 2, 3),
VALUES(NULL, 3' at line 3

you should only one keyword VALUES
INSERT INTO rented_video
VALUES (NULL, 1, 2),
(NULL, 2, 1),
(NULL, 2, 2),
(NULL, 2, 3),
(NULL, 3, 4),
(NULL, 3, 5),
(NULL, 4, 6),
(NULL, 4, 7),
(NULL, 4, 8),
(NULL, 5, 9);
SQLFiddle Demo

Related

Error in sql syntax while trying to perform query

INSERT INTO `2018_players`(`p_id`, `player_fname`, `player_lname`, `stats_id`, `home_id`) VALUES (
(1, 'LeBron', 'James', 2, 3, 23),
(2, 'Stephen', 'Curry', 4, 5, 30),
(3, 'James', 'Harden', 6, 7, 13),
(4, 'Giannis', 'Antekokounmpo', 8, 9, 34),
(5, 'Paul', 'George', 10, 11, 13);
Server version: 5.7.23
1 error were found during analysis.
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 7
You are specifying 5 columns, but trying to insert 6 values per row, and there is an extra bracket after VALUES, you want something like:
INSERT INTO `2018_players`(`p_id`, `player_fname`, `player_lname`, `stats_id`, `home_id`, `extra_column`) VALUES
(1, 'LeBron', 'James', 2, 3, 23),
(2, 'Stephen', 'Curry', 4, 5, 30),
(3, 'James', 'Harden', 6, 7, 13),
(4, 'Giannis', 'Antekokounmpo', 8, 9, 34),
(5, 'Paul', 'George', 10, 11, 13);

SQL query INSERT column count error

I have removed 'age_range' from this query.
INSERT INTO `filters` (`id`, `user_id`, `profession_preference`, `country`, `city`, `order_by`, `profession_orientation`, `age_range`, `distance_range`, `location_dating`) VALUES
(9, 20, 3, 'All Countries', '', 2, 1, '16,100', '0,500', 0),
(10, 12, 3, 'Egypt', '', 2, 1, '', '', 0),
(11, 19, 3, 'All Countries', '', 2, 1, '16,100', '0,500', 0),
(13, 20, 3, 'All Countries', '', 2, 1, '16,100', '0,500', 0),
(14, 20, 3, 'All Countries', '', 2, 1, '16,100', '0,500', 0),
(15, 20, 3, 'All Countries', '', 2, 1, '16,100', '0,500', 0),
(25, 121, 3, 'All Countries', '', 3, 1, '18,23', '0,500 ', 0),
(26, 316, 3, 'United States', '', 3, 1, '17,25', '0,500', 0);
I executed again and receive this error:
MySQL said: Documentation
#1136 - Column count doesn't match value count at row 1
When you insert a record, it matches the values in the VALUES list to the columns in the columns list by comma-separated position. So, this insert statement:
INSERT INTO `foo` (`A`, `B`, `C`)
VALUES ('valueA', 'valueB', 'valueC')
It will insert valueA into column A, valueB into column B, etc. because they match positions in their respective lists. If you remove B from the columns list and leave VALUES alone, it will not attempt to insert valueA into column A, but valueB into column C because they now match value positions, but it won't know what to do with valueC because there are now more values than columns, so since you removed the column from the second position, you would also need to remove the value from the second position.
So back to your query, you would need to determine which position age_range occupied in the columns list and remove the values from the same position in the values list.
Does that make sense?
You have 9 columns defined in your insert statement and you are trying to insert 10 values. you either need to add another column definition or remove from your values.
According to rule the column name define and provided values count should be same. In your case one column value in extra.
As the documentation says
"Column count doesn't match value count"
You specify 9 columns (id, user_id, profession_preference, country, city, order_by, profession_orientation, distance_range, location_dating) in your insert statement
and you are trying to insert 10 values.
You have to remove one value or add another column
Before removing column this is the script which will work
CREATE TABLE filters (id INT, user_id INT, profession_preference INT, country VARCHAR(50), city VARCHAR(50), order_by INT, profession_orientation INT, age_range VARCHAR(50), distance_range VARCHAR(50), location_dating INT);
INSERT INTO filters (id, user_id, profession_preference, country, city, order_by, profession_orientation, age_range, distance_range, location_dating) VALUES
(9, 20, 3, 'All Countries', '', 2, 1, '16,100', '0,500', 0),
(10, 12, 3, 'Egypt', '', 2, 1, '', '', 0),
(11, 19, 3, 'All Countries', '', 2, 1, '16,100', '0,500', 0),
(13, 20, 3, 'All Countries', '', 2, 1, '16,100', '0,500', 0),
(14, 20, 3, 'All Countries', '', 2, 1, '16,100', '0,500', 0),
(15, 20, 3, 'All Countries', '', 2, 1, '16,100', '0,500', 0),
(25, 121, 3, 'All Countries', '', 3, 1, '18,23', '0,500 ', 0),
(26, 316, 3, 'United States', '', 3, 1, '17,25', '0,500', 0);
Now since you removed age_range column, below script will work:
INSERT INTO filters (id, user_id, profession_preference, country, city, order_by, profession_orientation, distance_range, location_dating) VALUES
(9, 20, 3, 'All Countries', '', 2, 1, '0,500', 0),
(10, 12, 3, 'Egypt', '', 2, 1, '', 0),
(11, 19, 3, 'All Countries', '', 2, 1, '0,500', 0),
(13, 20, 3, 'All Countries', '', 2, 1, '0,500', 0),
(14, 20, 3, 'All Countries', '', 2, 1, '0,500', 0),
(15, 20, 3, 'All Countries', '', 2, 1, '0,500', 0),
(25, 121, 3, 'All Countries', '', 3, 1, '0,500', 0),
(26, 316, 3, 'United States', '', 3, 1, '0,500', 0);
I removed third last column from insert script.
Hope this helps!

Is my SQL wrong or is it sqlfiddle?

I'm trying to better wrap my head around PIVOT in MySQL. I made a test table and query in SQLfiddle but I'm not getting it to work. I get the error:
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'PIVOT ( COUNT p.[stat] FOR p.[title] IN (p.[title], p.[weight],
p.[length]' at line 3
Table and data:
CREATE TABLE pivoter (
amount int,
title varchar(20),
weight int,
length int,
stat varchar(10)
);
INSERT INTO pivoter VALUES (5, 'Blip', 13, 5, 'hold');
INSERT INTO pivoter VALUES (2, 'Crog', 10, 5, 'full');
INSERT INTO pivoter VALUES (2, 'Gump', 22, 1, 'hold');
INSERT INTO pivoter VALUES (10, 'Yark', 7, 12, 'fun');
INSERT INTO pivoter VALUES (0, 'Blim', 6, 1, 'full');
INSERT INTO pivoter VALUES (3, 'Fron', 5, 8, 'hold');
INSERT INTO pivoter VALUES (8, 'Xand', 2, 7, 'fun');
INSERT INTO pivoter VALUES (2, 'Xnor', 7, 9, 'hold');
INSERT INTO pivoter VALUES (4, 'Rugg', 7, 8, 'fun');
INSERT INTO pivoter VALUES (9, 'Lint', 4, 10, 'fun');
INSERT INTO pivoter VALUES (9, 'Pawn', 7, 2, 'hold');
INSERT INTO pivoter VALUES (0, 'Undr', 12, 11, 'hold');
INSERT INTO pivoter VALUES (14, 'Call', 1, 6, 'full');
Pivot Query:
select stat, title, weight, length, amount from pivoter p
PIVOT
(
COUNT p.[stat]
FOR p.[title]
IN (p.[title], p.[weight], p.[length], p.[amount])
) AS testedPivot;
In case you were wondering, no, the data isn't supposed to make sense. I just made garbage to try this out.
Is the problem with my query or sqlfiddle?
If it's my query what can I do to make this work?
If the problem is sqlfiddle, do you know of an alternative?
The query in the question looks like T-SQL syntax for Microsoft SQL Server.
This syntax is not valid in MySQL, for a couple of reasons.
PIVOT is not a valid keyword in MySQL.
And MySQL uses backticks to escape identifiers, not square brackets.

Transfer mySQL data structure into new relation database (comma separated to relation table)

I have a several mySQL tables where I have saved the relation ID of the child table comma separated. Now I have to transfer this entries into a new table where for each relation is one entry.
Is there an easy way to transfer import query into the correct format?
Here the data example, my old table (cat_projects) has the following entries I want convert:
-- export of table cat_projects
INSERT INTO `cat_projects` (`id`, `authors`) VALUES
(2, '4,1'),
(3, '0'),
(4, '8,4,1'),
(5, '13,12'),
(10, '19,4,1'),
(13, ''),
(14, ''),
(15, '28,27,25,12,9,1');
This entries I want just to write into the new relation table (cat_project_relation). The att_id links to the another table where I have save the settings of the old authors column:
-- att_id = 58
-- item_id = id
-- value_sorting = counting from 0 for each item_id
-- value_id = for each relation one entry value
INSERT INTO `cat_project_relation` (`att_id`, `item_id`, `value_sorting`, `value_id`) VALUES
(58, 2, 0, '4'),
(58, 2, 1, '1'),
(58, 3, 0, '0'),
(58, 4, 0, '8'),
(58, 4, 1, '4'),
(58, 4, 2, '1'),
(58, 5, 0, '13'),
(58, 5, 1, '12'),
(58, 10, 0, '19'),
(58, 10, 1, '4'),
(58, 10, 2, '1'),
(58, 13, 0, ''),
(58, 14, 0, ''),
(58, 15, 0, '28'),
(58, 15, 1, '27'),
(58, 15, 2, '25'),
(58, 15, 3, '12'),
(58, 15, 4, '9'),
(58, 15, 5, '1');
I hope it is clear what I try to achieve. Is it possible to do that directly in SQL or do I have to apply an external bash script?
Actually it wasn't as difficult as I thought to write a little bash script. And I guess its the easier solution then program something in SQL.
#!/bin/bash
# input table
table="(2, '4,1'),
(3, '0'),
(4, '8,4,1'),
(5, '13,12'),
(10, '19,4,1'),
(13, ''),
(14, ''),
(15, '28,27,25,12,9,1');"
# fixed attribute id
att_id=58
# read each line into an array
readarray -t y <<<"$table"
# for each array item (each line)
for (( i=0; i<${#y[*]}; i=i+1 ))
do
z=${y[$i]}
# split by comma into array
IFS=', ' read -r -a array <<< "$z"
# loop through each value
for (( j=0; j<${#array[*]}; j=j+1 ))
do
# remove all other characters then number
nr=$(echo ${array[$j]} | sed 's/[^0-9]*//g')
# each first value is the item_id
if [ $j -eq 0 ]
then
item_id=$nr
else
k=$(expr $j - 1)
value_id=$nr
# print output line by line
echo "($att_id, $item_id, $k, '$value_id')," >> output.txt
fi
done
done
The result will be as the on asked in the question.

what would be a good db design for database base email system?

I have a simple table like this in mind to create a db base email system,
is this the best approach ?
TABLE `message`
- id
- parent_id
- message
- subject
- created_on
- is_draft
- sender_profile_id
TABLE `email_message`
- id
- is_read
- is_deleted
- message_id
- profile_id
case 1: profile A sending email to profile B,
and B Replies back (one to one
communication)
INSERT INTO `message` (`id`, `parent_id`, `message`, `subject`, `created_on`, `is_draft`, `sender_profile_id`) VALUES
(1, 0, 'Hi what''s up how are u', 'Hi', '2010-12-08 11:27:54', 0, 1),
(2, 1, 'yeah i am gud', 0, '2010-12-08 11:28:19', 0, 2);
INSERT INTO `email_message` (`id`, `is_read`, `is_deleted`, `message_id`, `profile_id`) VALUES
(1, 1, 0, 1, 2),
(2, 1, 0, 2, 1);
case 2:
-Profile A sending email to profile B,C, D.
-Profile B repling back all to whole group.
-A replying again to whole group.
-C replies to A only
INSERT INTO `message` (`id`, `parent_id`, `message`, `subject`, `created_on`, `is_draft`, `receiver_profile_id`) VALUES
(3, 0, 'Hi what''s up how are u', 'Hi', '2010-12-08 11:27:54', 0, 1),
(4, 3, 'yeah i am gud.', 0, '2010-12-08 11:28:19', 0, 2),
(5, 3, 'why are u gud?', 0, '2010-12-08 11:28:19', 0, 1),
(6, 3, 'what?', 0, '2010-12-08 11:28:19', 0, 3);
INSERT INTO `email_message` (`id`, `is_read`, `is_deleted`, `message_id`, `profile_id`) VALUES
(3, 1, 0, 3, 2),
(4, 0, 0, 3, 3),
(5, 0, 0, 3, 4),
(6, 0, 0, 4, 1),
(7, 0, 0, 4, 3),
(8, 0, 0, 4, 4),
(3, 0, 0, 5, 2),
(4, 0, 0, 5, 3),
(5, 0, 0, 5, 4),
(6, 0, 0, 6, 1);
I feel like a single table would be a lot easier to impliment :
TABLE Message
- MessageId (GUID)
- ParentId (GUID)
- Subject
- Message
- To
- Sender
- CreatedOn
- isDraft
- isDeleted
- isRead
If you are sending an email to a group, just create multiple records with different "To" entries