Mysql Find if one column values are partially present in multiple rows - mysql

I have a Mysql table which contains the subjects of emails. It has 2 columns, id and title. Some subjects might contain Re:, some might contain Fwd: and so on. I would like to write a query which returns rows where the value of the title of one row is contained in some other row. It is not necessarily an exact match but like finding a haystack in the needle. I have tried multiple ways like using sub queries or self join, but to no success. Any help will be appreciated. Thanks in advance.

Related

Why I am getting 2 rows with same data as a result?

I am using sql join for refining some result but i am not getting my result as expected.
My query was
select order_detail.*, product.img1, product.added_by,
product.fa,product.name, commition.commission
from order_detail,product,commition
where order_detail.order_id='ODN314746M38'
and product.id=order_detail.product_id
and product.added_by='1'
and commition.subcat=product.subcat
As a result i should get only the one row but I am getting 2 rows with same result
My actual data table is
But I am getting 2 rows back as a result like
why this is happening?
Can anyone help me out?
There must be 2 rows in comition with a matching subcat. Add comition.id to the SELECT list to see this.
You will need to refine the relationship with the comition table so you only return the appropriate row for this order. Without knowing more about your table structure I can't be more specific.
It's also helpful to use ANSI JOIN as in the other answer. This makes it easier to see how all the tables relate to each other, and often makes it clearer where you're missing a condition.

Link Tables with non exact information only matching the last three last letters

I have searched Stack Overflow and cannot find a way to match two fields based on the only the last three letters of the second field. I am not great at VBS but can get by. Here are the details:
Microsoft Access 2010. This is and Aircraft registration database. The first 4 letters in a field have a consistent length in the first database. Eg.
FABC
GPJR
IDTC
GPPC
The intended join field in the second database can look like this:
CFABC
C-FABC
ABC
C-GPJR
GPJR
PJR
CGPJR
I just need to take the last three letters in the first table and the last three letters in the second table and match them. It will likely be a make table query.
Any help would be appreciated.
Jeff
The syntax is as such:
SELECT ID
FROM Table1, Table 2
WHERE (((Right([Table1].[ID],3))=Right([Table2].[ID],3)));

Mysql calculation with multiple summands not working

I have a query on two columns from two different tables (connected by a left join in my query) and I want to order the search results by the occurence of the term I am looking for. I came up with this as my sorting variable in the statement, which might not be elegant, but works fine:
((LENGTH(table1.column)-LENGTH(REPLACE(lower(table1.column),lower('$term'),'')))/LENGTH('$term') AS sort_frequency
$term is my search term and at the end of the query I do this: ORDER BY sort_frequency DESC.
Now comes the difficulty: the calculation works fine for both tables separately, but when I want to connect the two by addition, the results of table2 always come in front of the results of table1 and nothing is ordered by occurence. My statement looks like this:
(((LENGTH(table1.column)-LENGTH(REPLACE(lower(table1.column),lower('$term'),'')))/LENGTH('$term')) + ((LENGTH(table2.column)-LENGTH(REPLACE(lower(table2.column),lower('$term'),'')))/LENGTH('$term'))) AS sort_frequency
I need this calculation, because the search results come from two different tables, but shall be ordered together on one page (let's say: one table is about images with certain keywords and the second table is about videos with certain keywords, once I searched for a specific keyword I don't care whether it is an image or video, I want the one that fits my keyword query most).
Do you have any idea why the calculation does not work? What is my mistake? I have tried adding/removing brackets, but that does not help.
Any help would be appreciated,
Have you tried using a UNION to combine the two table before searching for occurrences? My Suggestion is to use something along the lines of:
SELECT((LENGTH(newcolumn)-LENGTH(REPLACE(lower(newcolumn),lower('$term'),'')))/LENGTH('$term') AS sort_frequency FROM table1.column UNION table2.column AS newcolumn

Fetching results from one table and put the corresponding values in another table

I was searching for a result, but I didn't find proper solutions.
So, I will put my problem across. Please tell, if its already listed in any of the topics !
I have one table, table123, which has columns like,
Old_variable
New_Variable
ValueDesc
Default_Value
There is another table, t6, which has columns like,
template_code
Paragraph_code
Old_Var
New_var
This table has values, multiple times, the same paragraph code, in other templates.
Now, I want to join these two tables, based on New_variable and New_var and I want a result like, which has template_code, paragraph_code, New_variable and the corresponding values of variable description and default value. And only the paragraphs, should be present only once. If it is present in one template, it need not come in other template.
Is this a possible query through MySQL?

Query for matching words across multiple records in sql

I am struggling to group records from the same table that have the same words together. Below is an illustration of what I mean.
Record 1- http://www.example.com/abdgc
Record 2- http://www.example.com/gshfd/df
My main interest here is to build a query that looks for a common word (in the example, "example") among multiple records and group all them by this common word.
Thank you for your help.