Mysql Join two tables on Like with Concat - mysql

I want to join two tables on a like clause. My schema is as the link:
Sql Fiddle 1
As you can see the result is empty.
But if I use like as follows:
Sql Fiddle 2
As you see I can get the results with name or des contains 'GRE'. So what's the problem here?
I searched the answer for a while, and found the suggested way to do this is the same as I did:
similar question
Any suggestions will be highly appreciated.

You are doing it correctly. you just have to use TRIM() to remove trailing whitespaces. Use it like
SELECT
a.id as app_id,a.app_name,a.des,
b.id as tag_id, b.name as tag_name
FROM aa_t_aaaa_app a
JOIN aa_t_aaaa_tag b
ON ((a.app_name LIKE CONCAT('%', TRIM(b.name) ,'%')) or (a.des Like CONCAT('%', TRIM(b.name) ,'%')))
order by a.id`

Related

how to write this query in correct syntax?

SELECT collegename(SELECT allotement.collegename,dean.id
FROM dean,allotement
WHERE allotement.city=dean.city
&&dean.collegename<>allotement.collegename
&&dean.id<>allotement.id)as t WHERE id=1
SELECT collegename from (
SELECT allotement.collegename, dean.id
FROM dean,allotement WHERE allotement.city=dean.city
and dean.collegename<>allotement.collegename
and dean.id<>allotement.id)
as t WHERE id=1
A few points to note here:
Treat sub-query as a table source from which you are retrieving the data. Thus, you need a from in the first line.
&& doesn't work in SQL. You have to write and instead.
In your case, writing as t is optional.
You can actually go through a pretty good link which I generally use to follow mySQL syntax, as it's a bit confusing, considering the fact that different SQL databases have a slight variation in syntax and functions available.
You can refer to the official mySQL docs here as well, if in case required.
TRY THIS: We can simply achieve that in following simple way even we don't need sub query for that:
SELECT a.collegename, d.id
FROM dean AS d
INNER JOIN allotement AS a ON a.city = d.city
AND d.collegename <> a.collegename
AND d.id <> a.id
WHERE d.id = 1

MySQL joins where one field has surrounding tags to be stripped

I have two MySQL tables, articoli and tabtranslations, and the join between them should be trivial
SELECT CodArt, Translation FROM articoli LEFT JOIN tabtranslations ON articoli.CodArt = tabtranslations.Chiave
BUT, while articoli.CodArt ha simple strings (A001, BS15, etc..), field tabtranslations.Chiave is filled with surronding tags like <CODART>A001</CODART>, <CODART>BS15</CODART> thus overcomplicating joins - and I cannot modify it...
Well, is there a way I can solve this problem? Thanks
A quick and dirty soulution would be like this:
SELECT CodArt, Translation
FROM
articoli LEFT JOIN tabtranslations
ON CONCAT('<CODART>', articoli.CodArt, '</CODART>') = tabtranslations.Chiave
I don't know, but maybe this will work?
SELECT CodArt, Translation
FROM articoli
LEFT JOIN tabtranslations ON tabtranslations.Chiave LIKE '%' + articoli.CodArt + '%'
Just in case 'CODART' isn't the only possible tag, you may want to use REGEXP in the join predicate, like so:
select *
from codart c
left join tabtranslations t
on t.chiave REGEXP CONCAT('<.*>', c.codart, '</.*>');
Here is a sample fiddle for you to try it out: http://sqlfiddle.com/#!9/d6c04/1

wildcard doesn't working?

SELECT _cotM.Customer_ID, _cotM.Material_ID
FROM dbo.COT_Monthly AS cot
INNER JOIN
dbo.vw_Dim_Material AS matr ON cot.Material_ID = matr.Material_ID
GROUP BY _cotM.Customer_ID, _cotM.Material_ID
I have sql code and have result 25855 rows
but when i add where matr.Brand <> '%VIT%' upper group by the result still 25855 rows.
but when i delete wildcard where matr.Brand <> 'VIT' the result became 25089.
i wandering why the result like this ?
is i am wrong using wildcard or else?
Thanks for answering.
Wildcards only work with LIKE clauses, so change the condition:
WHERE matr.Brand NOT LIKE '%VIT%'
as Matt says use where matr.Brand not like '%VIT%',read more about wildcard

Mysql Inner Join Issues

This gives me all my articles that contain both the mentioned entities.
SELECT COUNT(ArticlesEntity.article_id) AS article_count
FROM articles_entities ArticlesEntity
WHERE ArticlesEntity.entity_id IN ('ENTITY_ID_1','ENTITY_ID_2')
GROUP BY ArticlesEntity.article_id
HAVING article_count>=2
I now want to add something to this query that excludes any entity that has 'ENTITY_ID_3'.
I tried the following but it returned the same results:
SELECT COUNT(ArticlesEntity.article_id) AS article_count
FROM articles_entities ArticlesEntity
WHERE ArticlesEntity.entity_id IN ('ENTITY_ID_1','ENTITY_ID_2')
AND ArticlesEntity.entity_id NOT IN ('ENTITY_ID_3')
GROUP BY ArticlesEntity.article_id
HAVING article_count>=2
What am I missing / doing wrong?
Thanks in advance
Try something like:
SELECT COUNT(ArticlesEntity.article_id) AS article_count
FROM articles_entities ArticlesEntity
WHERE ArticlesEntity.entity_id IN ('ENTITY_ID_1','ENTITY_ID_2')
AND NOT EXISTS (
select 1
from articles_entities
where article_id = ArticlesEntity.article_id
and entity_id = 'ENTITY_ID_3')
GROUP BY ArticlesEntity.article_id
HAVING article_count>=2
There must be more to your query than what you're showing here, because from what you're showing in your question, the results should be identical for both queries (If entity_id is equal to either id_1 or id_2, it's already not equal to id_3).
Edit:
Sorry, just noticed two things.
1) in your problem statement you said the results are identical (as expected).
2) in your title, you say "Inner Join Issues", but... you ain't got no inner join.

SQL Syntax with regards to SELECT

I keep getting an error saying that a specific column, doesn't exist for b.BookCode But I am fully aware that it does exist! I just wanted to make sure after staring at this for so long that I am not missing something obvious.
SELECT AuthorLast, AuthorFirst, OnHand, Title
FROM (Inventory i, Author a, Book b)
WHERE (i.BookCode = b.BookCode AND b.AuthorNum = a.AuthorNum);
I'm very new to SQL so I'm unsure if my syntax is off, Also do I need parentheses around the columns that I mentioned in SELECT. I had parentheses around them at first and got an error and was confused as to why.
Thanks!
You are using the old join syntax, you should use INNER JOIN instead.
SELECT AuthorLast, AuthorFirst, OnHand, Title
FROM Inventory i
INNER JOIN Author a
USING (BookCode) -- You can also use ON, but USING remove the ambiguous columns
INNER JOIN Book b
USING (AuthorNum); -- Same thing here
If you get an error saying one column doesn't exist, well, you should double check it. MySQL isn't lying to you for the sake of it! You might do your query on an outdated database, for instance.
Minimally, you should change this to:
SELECT AuthorLast, AuthorFirst, OnHand, Title
FROM Inventory i, Author a, Book b
WHERE i.BookCode = b.BookCode AND
b.AuthorNum = a.AuthorNum;
... and this is assuming that the columns in the SELECT part of your select statement are not ambiguous (i.e., found in more than one of the tables in the FROM part).