Shouldn't I see multiple inserts right away? - mysql

I'm trying to insert in Table2 multiple rows with multiple columns from Table1, but while doing so, I'm editing some of the values going into Table2. Both are large tables (several million rows). Here's the query:
insert into table2 (board_id,title,content,domainurl)
select (id, replace(title, '_', ' '), description, CONCAT("http://somesite.com/", title))
from table1
When running this query, shouldn't the insert start happening right away (being that I'm not doing a join)? I've started the query several minutes ago (phpmyadmin still shows "loading") but I don't see any rows being added to table2...?
EDIT: The query just stopped with the message "#1241 - Operand should contain 1 column(s)". I have no idea what that means!

Try this:
INSERT INTO table2 (board_id,title,content,domainurl)
SELECT
id AS board_id,
REPLACE(title, '_', ' ') AS title,
description AS content,
CONCAT("http://somesite.com/", title) AS domainurl
FROM table1

As per here all I had to do is remove the parenthesis from the SELECT clause.
select id, replace(title, '_', ' '), description, CONCAT("http://somesite.com/", title)
worked perfectly and fast!

Related

mysql query - how to merge tables in another table

I have a problem joining tables in the result column. i have a working query which combine different tables using UNION but when i'm extending another table i got an error saying 'The used SELECT statements have a different number of columns'
this is my query:
(SELECT
IDNumber,
CONCAT(LastName, ', ', FirstName, ' ', Middle) as Name,
CONCAT(EmDesignation, ', ', Department) as Information,
Image,
v.PlateNo as PlateNumber
FROM
tblemployee as e, tblvehicle as v
WHERE
v.RFIDNo LIKE '6424823943'
AND
e.RFIDNo LIKE '6424823943')
UNION
(SELECT
IDNumber,
CONCAT(LastName, ', ', FirstName, ' ', Middle) as Name,
CONCAT(Course, ', ', Year) as Information,
Image,
v.PlateNo as PlateNumber
FROM
tblstudents as s, tblvehicle as v
WHERE
v.RFIDNo LIKE '6424823943'
AND
s.RFIDNo LIKE '6424823943')
I have problem with this. Continuation query above
UNION
(SELECT
Barrier
FROM
tblinformation as inf
WHERE
inf.RFIDNo IN (6424823943)
ORDER BY
AttendanceNo DESC LIMIT 1)
The error message is correct. Add NULLs to your second query to match the column number, and it will work.
For example:
SELECT
Barrier,
NULL,
NULL,
NULL,
NULL
FROM
tblinformation as inf
...
The error message states what the problem is. Just improve number of columns in SELECT and it will work correctly.

SQL Help | The select list for the INSERT statement contains fewer items than the insert list

Please see my query below,
insert into dbo.orderDetails(orderNo,clientId,productId,quantity)
values(' ee941422-5546-4d62-b5d6-60ecd13ca2b8 ')
select client_id,product_id,amount from dbo.cart
where client_id =' efc08f7c-fdfc-4712-9488-fc1c55acb95e ' ;
In this I want a static orderno and the rest should come from the a table(dbo.cart).when i execute my query its shows this error
There are more columns in the INSERT statement than values specified in the
VALUES clause. The number of values in the VALUES clause must match the
number of columns specified in the INSERT statement.
Any solution.
You can't have both VALUES and SELECT. If you want to insert static values, put it into the SELECT list.
insert into dbo.orderDetails(orderNo,clientId,productId,quantity)
select ' ee941422-5546-4d62-b5d6-60ecd13ca2b8 ', client_id,product_id,amount from dbo.cart
where client_id =' efc08f7c-fdfc-4712-9488-fc1c55acb95e ' ;
My issue has been resolved, Below is the query which is working for me
INSERT INTO Exam_Trigger(Questions,CAT_ID,TOPIC_ID,SCHEDULED_TYPE,UPDATE_USER,UPDATE_DT,CREATE_USER,CREATE_DT)
select id ,'2','2','Weekly','',curdate(),'',curdate() from ( select GROUP_CONCAT(id SEPARATOR ',')as id from(select id from question_data order by rand() limit 4)as ls) as ps

msql insert data into table based on values in two other tables

I am using MySQL and have a table (documentShips) that I want to store connections / links between documents and users.
The users table has columns including id, first_name and last_name etc...
The documents table has columns including id and users, where the users column contains a comma separated value
E.g. "Joe Bloggs, Fred Nerk, Simon McCool" etc...
I want to match users between the tables (documents and users) using a like statement, e.g.:
where documents.authors like '% users.last_name %'
and insert them into the documentShips table, e.g.:
insert into documentShips (user_id, document_id) ... values () ... where ...
I am struggling to create a valid (mysql) insert statement to do this.
Any help would be greatly appreciated !!!
Thanks,
Jon.
If I understand correctly you can use FIND_IN_SET() like this
INSERT INTO documentShips (user_id, document_id)
SELECT u.id, d.id
FROM documents d JOIN users u
ON FIND_IN_SET(CONCAT(u.first_name, ' ', u.last_name), d.authors) > 0
ORDER BY d.id, u.id
Here is SQLFiddle demo
In order for it to work correctly you have to make sure that comma separated values in document.authors have no spaces before or after commas. If in fact you have spaces then eliminate them first with a query like this
UPDATE documents
SET authors = REPLACE(REPLACE(authors, ' ,', ','), ', ', ',');
Here is SQLFiddle demo
Now consider to normalize your documents table.
Use INSERT...SELECT syntax as shown in the MySQL documentation. The documentation also has some examples.

Checking for doubled entries in value separated by commas?

Is there a way to get a value like this one: "300, 400, 500, 300" check each number separated with comma and if it is doubled delete it. So the value will look like this : "300, 400, 500".
I could do it in PHP script but I just wonder if it is possible using MySQL.
Create a temp table with unique index, insert values ignoring duplicate errors, select all records from the temp table, delete the table.
Quick play, but to get the unique values for each row you could use something like this
SELECT Id, GROUP_CONCAT(DISTINCT aWord ORDER BY aWord ASC)
FROM (SomeTable.Id, SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(concat(SomeColumn, ','), ' ', aCnt), ',', -1) AS aWord
FROM SomeTable
CROSS JOIN (
SELECT a.i+b.i*10+c.i*100 + 1 AS aCnt
FROM integers a, integers b, integers c) Sub1
WHERE (LENGTH(SomeColumn) + 1 - LENGTH(REPLACE(SomeColumn, ',', ''))) >= aCnt) Sub2
GROUP BY ID
This relies on having a table called integers with a single column called i with 10 rows with the values 0 to 9. It copes with up to ~1000 words but can easily be altered to cope with more
Probably easiest to use an INSERT / ON DUPLICATE KEY UPDATE to use this to make the values unique.

Mysql query returns a tremendously large file

I have the query to select some columns from two tables, which must contain some cells which are actually empty but have to name it in query such as
select name, REPLACE(format, ' – ', '/') as format, CAST(price AS UNSIGNED INTEGER) as price, CONCAT('http://example.com/',url_key) as url, level_id_value, delivery, sku from t1 where delivery <> 'slut' INTO OUTFILE 'test.txt' FIELDS TERMINATED BY '\t';
which perfectly works producing 2.5 MB size text file in lib folder but the other code as follows for another query where i have to add one column from another table along with some empty columns with some title, seems to work but produces a file size of around 11GB,
sELECT t1.sku, t.name, ' ' as Size, format, ' ' as subcategory, CAST(t1.price AS UNSIGNED INTEGER) as price, ' ' as dummy, CONCAT('http://example.com/',t1.url_key) as url, CONCAT('Från ',t1.level_id_value) as shipping_fee, t2.is_in_stock as stock, ' ' as text1, ' ' as Description from t2,t1 where t1.delivery <> 'slut' INTO OUTFILE 'test.txt' FIELDS TERMINATED BY '\t';
any help to debug this issue would be appreciable, don know much about mysql, so detailed explanation are highly welcome. Is there a way where i can make the code analogous to the first working code from two tables? I just need one column from t2 table, that is is_in_stock.
So the first one is a simple query SELECT INTO with a WHERE clause, this is going to return a small number of results.
The second one is showing an implicit inner join, but you arent returning the values based on a unique row, you just pull in everything from both tables, regardless of whether they are matching in both tables
SELECT t1.sku,
t.name,
' ' as Size,
format,
' ' as subcategory, CAST(t1.price AS UNSIGNED INTEGER) as price,
' ' as dummy, CONCAT('http://example.com/',t1.url_key) as url,
CONCAT('Från ',t1.level_id_value) as shipping_fee,
t2.is_in_stock as stock,
' ' as text1,
' ' as Description **`from t2,t1`**
where t1.delivery <> 'slut' INTO OUTFILE 'test.txt' FIELDS TERMINATED BY '\t';
This one is how I would do it, but you still need to isolate each record, so what makes each row unique, and do the inner join ON those fields between t1 and t2 like inner join t2 ON t1.name = t2.name and t1.id = t2.id but it should look something like this
SELECT t1.sku,
t1.name,
t1.format,
CAST(t1.price AS UNSIGNED INTEGER) as price,
CONCAT('http://example.com/',t1.url_key) as url,
CONCAT('Från ',t1.level_id_value) as shipping_fee,
'' as dummy,
'' as size,
'' as subcat,
'' as text1,
'' as Description,
t2.is_in_stock as stock,
from t1
INNER JOIN t2 WHERE t1.delivery <> 'slut'
INTO OUTFILE 'test.txt' FIELDS TERMINATED BY 't\';
Hope it helps!