I have two tables: table1 with field id and table2 with filed prv_id
Eg: table1: id
06G35ME
03G66UN
table2: prv_id
456P1BU-06G35ME
06G35ME
72006G35ME
4D022HO-06G35ME
06G35ME-06G35ME
07R63EV-06G35ME
06G36EV-06G35ME
247S4HO-06G35ME
MNG06G35ME
so i have to find all prv_id which are like id. I am using LIKE command to compare the fields but i am not getting any output. Please help
Pretty sure this will work in SQL Server and MySQL
select a.prv_id
from table1 a
left join table2 b
on a.prv_id like '%'+b.id+'%'
Related
Once I have the two tables compared and they give me what has changed, I would like the first table to be updated with the new data from the second one.
This is my code where I put in my phpmyadmin database that compares the two tables:
SELECT
codice_Fiscale,nome,cognome,etichetta,sesso,residenza,
cellulare,email,telefono,id_vitaever
FROM (
SELECT codice_Fiscale,nome,cognome,etichetta,sesso,residenza,
cellulare,email,telefono,id_vitaever
FROM operatore
UNION ALL
SELECT codice_Fiscale,nome,cognome,etichetta,sesso,residenza,
cellulare,email,telefono,id_vitaever
FROM operatoreImport
) tbl
GROUP BY codice_Fiscale,nome,cognome,etichetta,sesso,residenza,
cellulare,email,telefono,id_vitaever
HAVING count(*) = 1
ORDER BY codice_Fiscale
I have this tables:
TABLE 1:
id name
1 Oriol
2 Ricard
TABLE 2:
id name
1 Uriol
2 Ricard
And once compared the two tables, I want this:
TABLE 1:
id name
1 Uriol
2 Ricard
maybe this code help you
UPDATE table1 t1, table2 t2
SET t1.name=t2.name, t1.surname=t2.surname
WHERE t1.id=t2.id
Here's are very simplified versions of my tables.
[stock_adjust]
id
batch_table_name
batch_id
adjustment_reason
qty
[ingredient_batch]
id
batch_name
...lots of other columns
[product_batch]
id
batch_name
...lots of other columns
[packaging_batch
id
batch_name
...lots of other columns
stock_adjust contains the name of the correct batch table in the column batch_table_name which will either be "ingredient_batch", "product_batch" or "packaging_batch". I need to get the values from the correct batch table for each entry. The pseudo code for this would look something like the following:
SELECT sa.id, sa.adjustment_reason, sa.qty, batch.batch_name
FROM stock_adjust AS sa, [sa.batch_table_name] AS batch
WHERE sa.batch_id=batch.id
I have tried to simplify the description and tables as much as possible, hopefully I haven't simplified it too much and the above makes sense?
I have found several questions regarding similar issues to the following, but they either do not work correctly in this situation or I am not understanding the question correctly.
While I would recommend updating your database schema, here's an approach that could work for you given your scenario:
select sa.id, b.batch_name
from stock_adjust sa join (
select id, batch_name, 'ingredient_batch' table_name from ingredient_batch
union all
select id, batch_name, 'product_branch' table_name from product_branch
union all
select id, batch_name, 'packaging_batch' table_name from packaging_batch
) b on sa.batch_table_id = b.id and b.table_name = sa.batch_table_name
SQL Fiddle Demo
Maybee a dynamic query could be the solution. Check this answer SELECT * FROM #variable
I've got one col (state_not_allowed) in TABLE vendor_product where I'm trying to insert values from product_catalog_varchar.value - but only if there's a sku in vendor_product that matches a sku in product_catalog where product_catalog's id equals product_catalog_varchar's id and product_catalog_varchar's attribute id = 523.
I'm basically trying to do the MySQL equivalent of an Excel VLOOPUP. I need the result of the following query:
SELECT product_catalog_varchar.value
FROM product_catalog_varchar
JOIN product_catalog
ON product_catalog.id = product_catalog_varchar.id
JOIN vendor_product
ON vendor_product.sku = product_catalog.sku
AND product_catalog_varchar.attribute_id = 523
To be inserted in to column state_not_allowed, where the sku in vendor_product = the sku in product_catalog.
I've done some research on INSERT INTO, here and on Google in general. Looks like a lot of the instruction out there is on simplier queries so I haven't been able to find a decent model to figure out what to do. I can tell you that this query doesn't work:
INSERT INTO vendor_product(`state_not_allowed`)
SELECT product_catalog_varchar.value
FROM product_catalog_varchar
JOIN product_catalog
ON product_catalog.id = product_catalog_varchar.id
JOIN vendor_product
ON vendor_product.sku = product_catalog.sku
AND product_catalog_varchar.attribute_id = 523
It throws the following error: #1062 - Duplicate entry '' for key 2
And if I got to vendor_product and look, instead of simply inserting values in to state_not_allowed, it's creating a whole new row (with no data). Clearly, I'm misunderstanding in a fundamental sense here. Help me out? Thanks folks.
This query shows the general idea of what you want to do.
insert into table2
(field1)
select distinct field1
from table1
where field1 in
(select field1
from table1
except
select field1
from table2)
The details vary from RDBMS to RDBMS. For example, Oracle uses the keyword minus instead of except. Your MySql documentation will help you with the details.
Note that while it's tempting to simplify this by using "not in" that construct tends to be slow.
The query is
Select id from TableA
where typ_cd="NT"
and id not in
( select id from TableA where typ_cd="BB")
I need to find those id's whose type_cd ="NT" and compare those id's with the same table which are not present for type_cd="BB" .I pretty confused why the above query is not returning the correct values.
Edit: -I'm referencing the same table and there are no null values for the column ID
Please let me know how can i achieve the same result in sql server so that i can try to write an equivalent query in sybase
TableA
id typ_cd
1 NT
1 BB
3 NT
4 NT
4 BB
I need the id=1 as the result since the id=1 is present for typ_cd=NT but not for typ_cd=BB
But at present with the above query im getting null in sybase
Replace the double quotes with single quotes.
Select id from TableA
where typ_cd='NT'
and id not in
( select id from TableA where typ_cd='BB')
I am buidling an app in CakePHP. I have 2 models:
- Project
- User
The Project model has various belongsTo relations to the user model, one for the creator, one for the last editor and one for the manager. This works fine.
Then I add a virtual field to the User model, called 'name', which is CONCAT(first_name, ' ', last_name). It combines the first name and last name into a general name field, which is used througout the app.
After this, I get SQL errors saying that the first_name column is ambiguous. This is because in the query, the alias for Creator, Manager, etc is not used in the CONCAT field.
Any ideas on how to avoid this?
Showing the exact queries should help resolve this problem. But if you are joining 2 tables, and they both have a column with the same name. you have to reference the column with TableName.ColumnName, like this.
Select Table1.Column1 AS someColumn, Table2.Column1 AS SomeOtherColumn
FROM Table1
INNER JOIN Table2
ON Table1.ID = Table2.Table1ID
WHERE Table1.ID = 3
You can shorten this up by giving your tables aliases, As follows.
Select T1.Column1 AS someColumn, T2.Column1 AS SomeOtherColumn
FROM Table1 AS T1
INNER JOIN Table2 AS T2
ON T1.ID = T2.Table1ID
WHERE T1.ID = 3
I found the solution: http://book.cakephp.org/view/1632/Virtual-fields-and-model-aliases
Try specifying what table the first_name column is from. Something like this:
CONCAT(table1.first_name,'',table1.last_name)