Error in sql syntax while trying to perform query - mysql

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);

Related

MySQL: Join records 1:1 that have a one to many relationship

I have a table that contains properties and their types:
INSERT INTO properties (property_id, year_built, type_id, code)
VALUES
(1, 2000, 3, 'ABC'),
(2, 2001, 3, 'ABC'),
(3, 2002, 3, 'ABC'),
(4, 2003, 3, 'ABC'),
(5, 2004, 3, 'ABC'),
(6, 2005, 3, 'ABC'),
(7, 2000, 3, 'DEF'),
(8, 2001, 3, 'DEF'),
(9, 2002, 3, 'DEF'),
(10, 2003, 3, 'DEF'),
(11, 2004, 3, 'DEF'),
(12, 2005, 3, 'DEF'),
(13, 2000, 3, 'GHI'),
(14, 2001, 3, 'GHI'),
(15, 2002, 3, 'GHI'),
(16, 2003, 3, 'GHI'),
(17, 2004, 3, 'GHI'),
(18, 2005, 3, 'GHI');
I have a second table 'agents' with the same number of records as the properties table.
INSERT INTO agents (agent_id, year_built, type_id)
VALUES
(50, 2000, 3),
(51, 2001, 3),
(52, 2002, 3),
(53, 2003, 3),
(54, 2004, 3),
(55, 2005, 3),
(56, 2000, 3),
(57, 2001, 3),
(58, 2002, 3),
(59, 2003, 3),
(60, 2004, 3),
(61, 2005, 3),
(62, 2000, 3),
(63, 2001, 3),
(64, 2002, 3),
(65, 2003, 3),
(66, 2004, 3),
(67, 2005, 3);
There is a field in the properties table: 'agent_id' that should be populated with a single agent of the same year and type. For example, this is the expected result of the properties table for the year 2000 after running an update statement:
SELECT (*) FROM properties WHERE year_built = 2000;
property_id year_built type_id code agent_id
1 2000 3 ABC 50
7 2000 3 DEF 56
13 2000 3 GHI 62
Every join that I try results in all matching agent records returned for each property_id. For example:
SELECT properties.*, agents.agent_id
FROM properties
JOIN agents
USING(year_built, type_id)
WHERE properties.year_built = 2000;
Would give the result:
property_id year_built type_id code agent_id
1 2000 3 ABC 50
1 2000 3 ABC 56
1 2000 3 ABC 62
7 2000 3 DEF 50
7 2000 3 DEF 56
7 2000 3 DEF 62
13 2000 3 GHI 50
13 2000 3 GHI 56
13 2000 3 GHI 62
I'm aware that a simple join will return all the agent records, but I'm not sure how to match a single agent record to a single properties record with just the fields I have to work with. In addition, I would want these to be ordered - so that the first property_id for a year/type matches with the first agent_id of the same year/type. I should also add that neither table's fields, keys, or properties can be modified.
As the data from table properties can be evenly matched to the data from table agents, we can capitalize on the row number added to each table for precise matching. This is written and tested in workbench using MySQL5.7 :
select p.property_id,p.year_built,p.type_id,p.code,agent_id from
(select property_id,year_built,type_id,code,#row_id:=#row_id+1 as rowid
from properties,(select #row_id:=0) t ) p
join
(select agent_id,year_built,type_id,#row_number:=#row_number+1 as rownumber
from agents,(select #row_number:=0) t ) a
on p.year_built=a.year_built and p.type_id=a.type_id and p.rowid=a.rownumber
where p.year_built=2000
;

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.

Why doesnt this insert statement work?

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