זקי חלטורה 25.02.2310:00

mysql הוספת שורה לטבלה מילת מפתח union הודעת שגיאה

שלום,
קוד ליצירת טבלה בשם abcd דלהלן עובד:
[code]
USE skullcrashingwords;
DROP TABLE IF EXISTS abcd;
CREATE TABLE abcd(
ID INT AUTO_INCREMENT,
Name VARCHAR(255) not null collate utf8_unicode_ci,
KEY (ID)
);

INSERT INTO abcd(name)
SELECT 'aaaaa';
[/code]
נוצרה לי טבלה בשם abcd בעלת שורה אחת המכילה שדה מספר רץ ושדה name עם ערך aaaaa.
כאשר אני רוצה להוסיף יותר משורה אחת לטבלה, 2 שורות, בעזרת מילת המפתח union אני חוטף הודעת שגיאה ! להלן הקוד המתוקן
[code]
USE skullcrashingwords;
DROP TABLE IF EXISTS abcd;
CREATE TABLE abcd(
ID INT AUTO_INCREMENT,
Name VARCHAR(255) not null collate utf8_unicode_ci,
KEY (ID)
);

INSERT INTO abcd(name)
SELECT 'aaaaa';

UNION
SELECT 'bbbb';
[/code]
להלן נוסח השגיאה שחטפתי עם הוספת מילת המפתח union לקוד שעבד:
[code]
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION
SELECT 'bbbb'' at line 1
[/code]
מישהו יוכל להסביר לי מדוע union לא עובד אצלי?
תודה
Sharpy 25.02.2314:02
1. chatgpt answer: בתגובה להודעה מספר 0
The reason why you are getting an error message when using UNION in your SQL code is that UNION should be used between two SELECT statements to combine the results of both queries into a single result set. However, in your code, you are using UNION after the first SELECT statement, which is not a valid syntax.

To fix the issue and add more rows to your abcd table using UNION, you should use the following code instead:

INSERT INTO abcd(name)
SELECT 'aaaaa'
UNION
SELECT 'bbbb';

This code will select two rows, 'aaaaa' and 'bbbb', and combine them into a single result set using UNION, which will then be inserted into the abcd table.

Note that the SELECT statements used in the UNION must have the same number of columns and compatible data types. In this case, both SELECT statements only select one column, 'aaaaa' and 'bbbb', which are both strings, so they are compatible and can be combined using UNION.
זקי חלטורה 25.02.2316:32
2. auto_increase field is not implicity increase. בתגובה להודעה מספר 1
and UNION AMONG dozens of selects works in other scripts
העבר לפורום אחר
העבר לפורום:
סיבה:
תגובה חדשה
כותרת:
תוכן:
סמיילים:
הצג
עריכת אשכול
כותרת:
תוכן:
סמיילים:
הצג