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

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.

Related

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

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.

Selecting rows based on duplicate values

I basically have results grid and i have a drop down menu on my application which filters the 'Carrier' column. But when selecting a certain carrier I want all the rows returned that have the same dr_id as the Carrier which has been selected.
For example if you look at the picture attached it shows my results grid. If I filter by carrier 'ACE CALL LTD_UK' then I want rows 27, 28, 29 and 30 returned because the dr_id is the same.
Thanks
I don't have a complete solution for you as I don't know exactly what you database schema is (and it is a large stored procedure!). However I do have some suggestions/comments which you might find helpful:
I assume that the stored procedure will currently be returning a single row when they filter is set to 'ACE CALL LTD_UK', if not then this might not be relevant!
What I would do in this case would be to take you SELECT statement and put the results into a CTE, temporary table or nested query. (I'm not sure what SQL DBMS your using, looks like MSSQL, but you also have a MySQL tag for you post).
Once I have those results I would then use a LEFT JOIN from the dr_id in the temp table back to the drm table on the same column. From here you will again need to join to other tables where the data is not distinct, for example the Carrier table, then select the columns that you require.
You could do all this in the existing SELECT statement, however you will have to start joining on tables multiple times or use nested queries, which would get very messy. However the main reason why I chose the solution I have posted is because I don't know the stored procedure well enough and therefore I chose the safest solution.
If you want an example of what I mean, I will try and provide one.

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

Run Query for each result of another query - Access

I am trying to use the results of another query to use as a criteria for another. In my specific example, I might have four houses that are 'A', 'B', 'C', 'D' (the unique values of a field in a table called Homes).
I want to go through another query and say for each house type, what percent of residents (in Residents table) are married, which I want to do by using Count() to count the number for each Home type.
Do I need to loop through the results using VBA? Asking on a higher level, is there a way to use the results from a query as inputs into another - more than just limit the results of the new query to the results of the prior query?
Edit:
In semi-pseudo code:
For each (result of previous query) Do
New query WHERE field1 = (row of previous query)
End Do
What I am trying to ask, is there a way to accomplish this in Access using SQL? Or is this something that has to be done in VBA?
I know that if it can be done in SQL that would be the best performing and best practice, but I'm relatively inexperienced in SQL and online resources aren't always helpful because Access has it's own particular flavor of SQL.
Since you are using VBA to run this, you can loop through your recordsets and yes you can use a value from one query in the next query. There are alot of resources out there to help.
VBA: Working with RecordSets
Looping through Record Sets
Code through all records
To answer your general question, yes there is. You can do a nested query i.e. select column a from table a where column a = (select column b from table b where column b=x)
You can go as many levels deep as you want, but the caveat is the nested query can only return one column and with a specific answer set. You can also use select statements as your columns i.e
select (select column b from table b) col b from table a ..... Not the exact syntax but I would have to dig out some examples from an old project to find that.
Nested queries are useful, but for the level of precision you are looking for, a stored procedure or a view is probably a better option. Just for ease of use, I would look at creating a view of the data that you want and then querying from that to start with. More flexible than a nested query.
You need to join two tables using a common column and then get your specific column from any of the table
SELECT A.REQUIRED_FIELD from TABLEA AS A
INNER JOIN TABLEB AS B ON A.FOREIGN_KEY=B.FOREIGN_KEY
WHERE CONDITION

How to get information about used tables from processed query?

Previous programmer left me with "beautiful" piece of code and he kind of forgot to apply something to it. There is a query which selects several items from several tables.
6 Items can be chosen. It means 6 tables can be chosen, however there can be more tables - even 20 of them. I need to get that list of tables from processed query.
Indeed, is it possible at all? Is there any command to get a list of tables the query used?
I'm unsure if I am understanding the question correctly but it may be worth 'explaining' the query and seeing which tables are being used as below.
EXPLAIN SELECT * FROM table1 JOIN
table2 USING (id)