I have this table and I am trying to use self join.
I am able to achieve only 50% of the expected results.
create table auto_correct(spell varchar(255), ne varchar(255));
insert into auto_correct values ('test', 'testing');
insert into auto_correct values ('adn', 'and');
insert into auto_correct values ('and', 'andru');
insert into auto_correct values ('testing', 'tested');
insert into auto_correct values ('this', 'that');
insert into auto_correct values ('junk', 'delete');
spell
ne
test
testing
adn
and
and
andru
testing
tested
this
that
junk
delete
The word 'test' is linked to 'tested' through 'testing'
The word 'adn' is linked to 'andru' through 'and'
How do I find all such linked records and display along with non-linked rows?
The expected results are:
spell
ne
test
tested
adn
andru
this
that
junk
delete
I have tried this:
select b.spell, a.ne
from auto_correct as a
left join auto_correct as b on a.spell = b.ne
where b.spell is not null;
returns:
adn
andru
test
tested
The rest of the records where no match is found are missing from above output.
WITH RECURSIVE
cte AS ( SELECT *
FROM auto_correct
WHERE NOT EXISTS ( SELECT NULL
FROM auto_correct ac
WHERE auto_correct.spell = ac.ne )
UNION ALL
SELECT cte.spell, auto_correct.ne
FROM cte
JOIN auto_correct ON cte.ne = auto_correct.spell )
SELECT *
FROM cte
WHERE NOT EXISTS ( SELECT NULL
FROM auto_correct
WHERE auto_correct.spell = cte.ne )
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=e90235ce5dde96a7255f8afe1a19d334
Related
I need to insert a table with some values (eg: 'NDA' in this case). This seems to work well if I have just one value to be inserted. I have around a dozen of similar values, is there a was i can tweak this query to insert say { 'NDA', 'SDM', 'APM' } values. Was curious to know if it can be done without a stored procedure or copy pasting the same statements over and changing the values.
INSERT IGNORE INTO customer_feature (customer_id, feature)
SELECT c.id, 'NDA' FROM
customer as c
where c.edition = 'FREE_TRIAL';
Reference: mysql -> insert into tbl (select from another table) and some default values
Is this what you want?
INSERT IGNORE INTO customer_feature(customer_id, feature)
select c.id, f.feature
from customer c cross join
(select 'NDA' as feature union all select 'SDM' union all select 'APM'
) f
where c.edition = 'FREE_TRIAL';
I'm not to familiar with SQl or Access.
I'm writing a INSERT INTO query in Access (2010) version of SQL.
I'm getting the following error (Query input must contain at least one table or query)
Here is my code
INSERT INTO TABLE ( AsOf, Portfolio, [String A], [Number A] ) VALUES
(#DATE#,
(SELECT P.Portfolio FROM tb_data_Portfolio P
WHERE IIF ( EXISTS
(SELECT Portfolio FROM tb_data_Portfolio p2 WHERE p2.Portfolio = 'Some String'),
(P.Portfolio = 'Some String'),
(P.Id = (SELECT px.Portfolio_Id FROM tb_sys_Portfolio_xRef px WHERE px.ext_Portfolio = 'Some String'))
)
)
,'String a', 'Number a');
Your query should start like this (you can't mix values and SELECT in an INSERT)
INSERT INTO TABLE ( AsOf, Portfolio, [String A], [Number A] )
SELECT DATE, P.Portfolio FROM tb_data_Portfolio P
WHERE ...;
I don't understand what you're trying to do in the WHERE clause, but I don't think you can use a subquery inside IIF(). Please explain your objectives and I'll edit my answer.
In 1 table I need to insert specific rows with specific values, according to other tables values.
UPDATE item_properties2 AS P
INNER JOIN items ON items.id = P.item
INNER JOIN item_groups ON item_groups.idp = items.group_id
SET P.nr = '0'
WHERE P.type = 1140614900 AND items_groups.idp = '1140503406';
This updates the table. But what I need is basically.
(id, type, item, value, shows, nr) VALUES
(78173, 1336983282, 1352706945, 'test Laisvai pastatomas Sharp', 0, 1)
item_properties2.Id - just row id,
item_properties2.type - connect to item_property_groups2.id ,
'item_properties2.item' this connects to item.id ,
item.id have another column which is item.group_id ,
item.group_id is connected to item_groups.id which have another column naked item_groups.idp .
I need to only select items_groups.idp = '1140503406' . And basically i it should add rows in item_properties2 with with specific type value which i entered and only to models with specific item_properties2. item accord to item_groups.idp . I don't know how to do it.
there are some ways to insert a bulk of data from 2 tables into a newer table.
this can be done like this:
INSERT INTO item_properties2 (id, type, item, value, shows, nr)
SELECT Yid, Ytype,Yitem,Yvalue, Yshows, Ynr
FROM items
INNER JOIN item_groups ON item_groups.idp = items.group_id
WHERE items_groups.idp = '1140503406'
Instead of using the VALUES tag, you will now call for a specific select statement. you only need to change the Y values above into your own column names.
Or you could place your input into a stored procedure
the above query will run over your 2 tables and will insert the values that go with the where statement into the 3th table.
CREATE PROCEDURE sInput
( #val1 int, #val2 int, #val3 varchar(10))
AS
INSERT INTO MyTable(val1, val2, val3)
VALUES(#val1, #val2, #val3)
return
I'm sure this is easy, but so help me I can't figure out why I can't return the right result.
Pretty standard setup, I have a ref_product table, a ref_tagmap table and a ref_tag table...
CREATE TABLE `ref_product` (
`id` DOUBLE ,
`name` VARCHAR (765),
`familyid` DOUBLE );
INSERT INTO `ref_product` (`id`, `name`, `familyid`) VALUES('264','Old Red Fixture 1','4');
INSERT INTO `ref_product` (`id`, `name`, `familyid`) VALUES('30206','Modern Red Fixture 2','405');
CREATE TABLE `ref_tag` (
`TagID` DOUBLE ,
`TagName` VARCHAR (150));
INSERT INTO `ref_tag` (`TagID`, `TagName`) VALUES('103','Modern Contemporary');
INSERT INTO `ref_tag` (`TagID`, `TagName`) VALUES('131','Red');
CREATE TABLE `ref_tagmap` (
`MapID` DOUBLE ,
`tagid` DOUBLE ,
`containertype` VARCHAR (45),
`containerid` DOUBLE );
INSERT INTO `ref_tagmap` (`MapID`, `tagid`, `containertype`, `containerid`) VALUES('17035','131','PROD','264');
INSERT INTO `ref_tagmap` (`MapID`, `tagid`, `containertype`, `containerid`) VALUES('17747','131','PROD','30206');
INSERT INTO `ref_tagmap` (`MapID`, `tagid`, `containertype`, `containerid`) VALUES('31959','103','PROD','30206');
Querying these tables using:
SELECT DISTINCT ref_product.familyid,ref_tag.tagid
FROM (ref_tag,ref_product)
JOIN ref_tagmap AS mt2 ON mt2.containerid=ref_product.id
AND mt2.containertype='PROD'
AND mt2.tagid=ref_tag.tagid
AND ref_tag.tagname='red'
correctly returns all of the product familyids that have the tag 'red' mapped to them. Similarly:
SELECT DISTINCT ref_product.familyid,ref_tag.tagid
FROM (ref_tag,ref_product)
JOIN ref_tagmap AS mt1 ON mt1.containerid=ref_product.id
AND mt1.containertype='PROD'
AND mt1.tagid=ref_tag.tagid
AND LCASE(ref_tag.tagname)='modern contemporary'
correctly returns the product familyids that have the tag 'modern contemporary' mapped to them. QUESTION IS, HOW DO I RETURN A LIST OF ONLY THE PRODUCT FAMILYIDS THAT HAVE BOTH TAGS MAPPED TO THEM?
I'm trying this, and it returns empty:
SELECT DISTINCT ref_product.familyid,ref_tag.tagid
FROM (ref_tag,ref_product)
JOIN ref_tagmap AS mt2 ON mt2.containerid=ref_product.id
AND mt2.containertype='PROD'
AND mt2.tagid=ref_tag.tagid
AND ref_tag.tagname='red'
JOIN ref_tagmap AS mt1 ON mt1.containerid=ref_product.id
AND mt1.containertype='PROD'
AND mt1.tagid=ref_tag.tagid
AND LCASE(ref_tag.tagname)='modern contemporary'
I have to assume I'm missing something fundamental here...feeling dense. Please help.
Thanks!
The typical way to do this is to ensure that the number of distinct items in the tag table is equal to the number of tags you wish to isolate.
Example:
SELECT p.familyid
FROM ref_product p
JOIN ref_tagmap tm ON tm.containerid=p.id
AND tm.containertype='PROD'
JOIN ref_tag t ON t.tagid = tm.tagid
AND t.tagname IN ('red',
'modern contemporary')
GROUP BY p.familyid
HAVING count(DISTINCT t.tagid) = 2;
In action: http://sqlfiddle.com/#!2/f377e/7
Is there a way to update multiple rows with different values for each row using a single SQL query? I have to update one colum in many rows with different data. Using individual update queries for each row seems excessive so if it's possible I would like to consolidate this process into a single SQL statement or at least reduce the number of queries required.
I am using PHP with the Zend framework and MySql.
Create a temporary table and fill it with:
CREATE TEMPORARY TABLE temptable (id INTEGER, VALUE VARCHAR(200))
INSERT
INTO temptable
VALUES
('1', 'val1'),
('2', 'val2'),
('3', 'val3'),
('4', 'val4')
Then issue:
UPDATE
mytable m, temptable t
SET m.value = t.value
WHERE m.id = t.id
Don't know about MySQL specifically, but to update multiple rows based on a SELECT, or a UNION of multiple SELECTs, I would do
UPDATE U
SET MyColumn = T.OtherColumn
FROM MyUpdateTable AS U
JOIN
(
SELECT [OtherColumn] = OtherColumn1
FROM MyOtherTable1
WHERE ...
UNION
SELECT OtherColumn2
FROM MyOtherTable2
WHERE ...
) AS T
ON T.ID = U.ID
Update 10/28/2014, converted to work for MySQL:
UPDATE MyUpdateTable AS U
JOIN
(
SELECT [OtherColumn] = OtherColumn1
FROM MyOtherTable1
WHERE ...
UNION
SELECT OtherColumn2
FROM MyOtherTable2
WHERE ...
) AS T
ON T.ID = U.ID
SET MyColumn = T.OtherColumn
I know this works for SQL Server, so it's worth a try in MySQL.
update xtable
set a =
Case
when a = "a"
then z
when a = "b"
then y
End
where ...
You can construct the case statement based on your different rows.