MySQL LIKE Statement not working - mysql

I'm trying to copy data from one table to another.
Here is my statement:
UPDATE tblMerchants T,
retailers R
SET T.linklabel = R.linklabel,
T.logo_image = R.logo_image,
T.screen_image = R.screen_image,
T.category = R.category,
T.meta_description = R.meta_description,
T.meta_title = R.meta_title,
T.meta_keywords = R.meta_keywords,
T.intro = R.intro,
T.permalink = R.permalink,
T.excerpt = R.excerpt,
T.main_link = R.main_link,
T.related_blog_post = R.related_blog_post,
T.active = R.active,
T.homepage_featured = R.homepage_featured
WHERE T.homepageurl LIKE '%R.linklabel%'
For example, T.homepageurl would look like http://www.amazon.com/ and R.linklabel would look like amazon.com. So I can't figure out why its not working. I'm not getting any errors, its just saying 0 rows affected.

You should be able to use CONCAT to do this:
WHERE T.homepageurl LIKE CONCAT('%', R.linklabel, '%');
The concat function is used to concatenate multiple strings together. The reason why it's not working is because it's trying to match "http://www.amazon.com" with "%R.linklabel%" instead of "amazon.com".

Related

Sql Alchemy filter / where clause joined by OR not AND

I want to select a bunch distinct records based off a composite key. In SQL I'd write something like this:
SELECT * FROM security WHERE (
exchange_code = 'exchange_code_1' AND code = 'code_1')
OR (exchange_code = 'exchange_code_2' AND code = 'code_2')
...
OR (exchange_code = 'exchange_code_N' AND code = 'code_N')
)
With SQLAlchemy I'd like to use the filter clause like:
query = sess.query(Security)
[query.filter(
and_(Security.exchange_code == security.exchange_code,
Security.code == security.code)
) for security in securities]
result = query.all()
The problem is filter and where join clauses with an AND not an OR... is there some way to use filter with OR?
Or is my only choice to generate a bunch of individual select's and UNION them? Something like:
first = exchanges.pop()
query = reduce(lambda query, exchange: query.union(exchange.pk_query),
first.pk_query())
query.all()
Use or_:
query = sess.query(Security).filter(
or_(*(and_(Security.exchange_code == security.exchange_code,
Security.code == security.code)
for security in securities)))
If your database supports it, you should use tuple_ instead.

SQL BINARY Operator not showing exact match

I have a table with the following columns...
[Name] = [Transliteration] = [Hexadecimal] = [HexadecimalUTF8]
...with multiple rows of UTF-8 characters, such as:
ङ = ṅa = 0919 = e0a499
ञ = ña = 091e = e0a49e
ण = ṇa = 0923 = e0a4a3
न = na = 0928 = e0a4a8
In order to search for the row that exactly matches ña in the Transliteration column , I enter the following command:
SELECT DISTINCT * FROM Samskrta
WHERE BINARY (Transliteration = concat(0xc3b161))
ORDER BY HexadecimalUTF8;
...which produces 4 rows.
Why is the SQL command not producing only the row that exactly matches ña?
What SQL command produces only the row that exactly matches ña?
The following command produces the same results:
SELECT DISTINCT * FROM Samskrta
WHERE BINARY (Transliteration = 'ña')
ORDER BY HexadecimalUTF8;
FIRST OF ALL, your query can't work as indicated: you are applying BINARY() to the result of the logical comparison, NOT comparing the BINARY() of whatever to whatever.
Try reproducing your code PRECISELY if you expect people to be able to tender assistance.

HOW do i do a data append against multiple columns

I added a column into table A and right now it is empty. What i want to do is take the phone column from table consumer and input it into appphone of table diabetic as long as the first name, last name, address, city, state, and zip, match up in both tables. Below is the query i have been trying and in theory should work but is not. I keep getting the same error no matter which way i change the query.--
error 'subquery returns more than one row'
UPDATE Diabetic_DB
SET Diabetic_DB.AppPhone = (SELECT Consumer.PHONE FROM Consumer
WHERE Consumer.FN = Diabetic_DB.FirstName
and Consumer.LN = Diabetic_DB.LastName and Consumer.ADDR = Diabetic_DB.Address1
and Consumer.CITY = Diabetic_DB.City and Consumer.ST = Diabetic_DB.State
and Consumer.ZIP = Diabetic_DB.Zip)
WHERE EXISTS (SELECT DISTINCT(PHONE) FROM Consumer WHERE Consumer.FN = Diabetic_DB.FirstName
and Consumer.LN = Diabetic_DB.LastName and Consumer.ADDR = Diabetic_DB.Address1
and Consumer.CITY = Diabetic_DB.City and Consumer.ST = Diabetic_DB.State
and Consumer.ZIP = Diabetic_DB.Zip)
the original query i ran looked like this.
UPDATE Diabetic_DB
SET Diabetic_DB.AppPhone = Consumer.PHONE
WHERE EXISTS (SELECT * FROM Consumer WHERE Consumer.FN = Diabetic_DB.FirstName
and Consumer.LN = Diabetic_DB.LastName and Consumer.ADDR = Diabetic_DB.Address1
and Consumer.CITY = Diabetic_DB.City and Consumer.ST = Diabetic_DB.State
and Consumer.ZIP = Diabetic_DB.Zip)
Try something like this:
UPDATE Diabetic_DB
INNER JOIN Consumer ON
Consumer.FN = Diabetic_DB.FirstName
and Consumer.LN = Diabetic_DB.LastName and Consumer.ADDR = Diabetic_DB.Address1
and Consumer.CITY = Diabetic_DB.City and Consumer.ST = Diabetic_DB.State
and Consumer.ZIP = Diabetic_DB.Zip
SET Diabetic_DB.AppPhone = Consumer.PHONE

NHibernate CreateSqlQuery and addEntity

The hibernate manual says this:
String sql = "SELECT ID as {c.id}, NAME as {c.name}, " +
"BIRTHDATE as {c.birthDate}, MOTHER_ID as {c.mother}, {mother.*} " +
"FROM CAT_LOG c, CAT_LOG m WHERE {c.mother} = c.ID";
List loggedCats = sess.createSQLQuery(sql)
.addEntity("cat", Cat.class)
.addEntity("mother", Cat.class).list()
Now, what I have is basically the same. I am return two of the same type per row. I am doing a select something like this:
SELECT {ctrl1.*}, {ctrl2.*} FROM tableA AS A
LEFT JOIN tableB AS ctrl1 ON (A.controlID = ctrl1.controlID AND ctrl1.controlOptionType = ? AND ctrl1.controlOptionValue = ?)
LEFT JOIN tableB AS ctrl2 ON (A.controlID = ctrl2.controlID AND ctrl2.controlOptionType = ? AND ctrl2.controlOptionValue = ?)
And then I addEntity("ctrl1", typeof(mycontrolclass) and
addEntity("ctrl1", typeof(mycontrolclass)
Which seems exactly the same to me as their example. But I get this exception:
"Could not execute query" and the inner exception is "Could not find specified column in results".
If I copy the sql in the exception(to which it has added "AS ctrl1_1_3_3_" etc) it works fine.
Thanks.
What exactly are you trying to do? I believe you might not need using either of them.
// Using HQL:
var motherId = 25;
var hql = "select c.birthDate, c.mother from Cat c where c.mother.Id = :motherId";
var result = Session.CreateQuery(hql)
.SetParameter("motherId", motherId)
.ToList();
// Using NHibernate.LINQ:
var result = (from cat in Session.Linq<Cat>()
where cat.Mother.Id == motherId
select new { cat.birthDate, cat.mother }).ToList();
HQL query examples.
LINQ for NHibernate examples.
I dealt with your problem just for studying purposes, because you will surely
have found a solution in the meanwhile, but the problem should not lie in
the query (which is ok), but in some mapping inconsistency or somewhere else
(perhaps Database).

updating a column with avg data from another table column

i wrote a command like this to update a column in one table with avg of columns from another table.. its giving errors
UPDATE college_rating,products set
property1_avg = avg(college_rating.rating1),
property2_avg = avg(college_rating.rating2),
property3_avg = avg(college_rating.rating3),
property4_avg = avg(college_rating.rating4),
property5_avg = avg(college_rating.rating5),
property6_avg = avg(college_rating.rating6),
property7_avg = avg(college_rating.rating7),
property8_avg = avg(college_rating.rating8),
property9_avg = avg(college_rating.rating9),
property10_avg = avg(college_rating.rating10),
property11_avg = avg(college_rating.rating11),
property12_avg = avg(college_rating.rating12),
property13_avg = avg(college_rating.rating13),
property14_avg = avg(college_rating.rating14),
property15_avg = avg(college_rating.rating15)
where products.alias = concat(college_rating.property1,'-',college_rating.property2,'-',college_rating.property3)
group by college_rating.property1,college_rating.property2, college_rating.property3
The MySQL multi-table update syntax does not allow the use of group by.
You can accomplish what you are trying to do by moving the aggregation into a sub-query and joining to that sub-query in the multi-table-update instead.
Something like this should work:
update products p
inner join (
select concat(property1,'-',property2,'-',property3) as alias,
avg(rating1) as property1_avg,
avg(rating2) as property2_avg,
avg(rating3) as property3_avg,
avg(rating4) as property4_avg,
avg(rating5) as property5_avg,
avg(rating6) as property6_avg,
avg(rating7) as property7_avg,
avg(rating8) as property8_avg,
avg(rating9) as property9_avg,
avg(rating10) as property10_avg,
avg(rating11) as property11_avg,
avg(rating12) as property12_avg,
avg(rating13) as property13_avg,
avg(rating14) as property14_avg,
avg(rating15) as property15_avg
from college_rating
group by property1,property2, property3
) as r on r.alias = p.alias
set p.property1_avg = r.property1_avg,
p.property2_avg = r.property2_avg,
p.property3_avg = r.property3_avg,
p.property4_avg = r.property4_avg,
p.property5_avg = r.property5_avg,
p.property6_avg = r.property6_avg,
p.property7_avg = r.property7_avg,
p.property8_avg = r.property8_avg,
p.property9_avg = r.property9_avg,
p.property10_avg = r.property10_avg,
p.property11_avg = r.property11_avg,
p.property12_avg = r.property12_avg,
p.property13_avg = r.property13_avg,
p.property14_avg = r.property14_avg,
p.property15_avg = r.property15_avg;
What is the error that you get? And you need to have a WHERE clause unless you want the UPDATE query to apply to ALL the records
I think you'd need to use sub queries, and I'm not sure if you can update two tables like that in MySQL, at least not without prefixing the attributes.