Stuck with a multi-part MySQL query - mysql

I am stuck with a MySQL query. I have tried a lot of ways but no luck so far. I'm still trying but I need a tip to follow the right path. The query is to get data from 3 tables based on some conditions. All is going well, just give me a hint on this part of the query.
select id,userid,amount from coins where id in (3,4)
This gives me:
id | userid | amount
3 | 2 | 900
4 | 3 | 1100
I want to get the record that has the maximum amount, ie here 1100, but I want to keep the where condition at the end (3,4), as it is the nested part, coming from another query (another table). So basically I need a solution that is in the same query.
If my question is not clear, please let me know, I will add the whole query and all tables data. Any help will greatly appreciated.

This should do the trick;
select id,userid,amount from coins where id in (3,4) order by amount DESC limit 1

Related

Using Lookup and getting a count from each Dataset

I have two datasets and I'm using Lookup to get one result, but the total is only from one dataset. I've tried and tried, but I'm having no luck.
First Dataset is called MedCond
This is the Data:
Drug_Name
Start_Date
Stop_Date
InmateID
Drug_Indication
Created
ID
Second Dataset is called ProblemList
This is the Data:
Medical_Condition
Inmate_ID
Created
ID
Drug Indication and Medical Condition are the same. I want to get a combined total of both.
This only gives me the count of the Drug Indications (which I have them grouped on) =Count(Lookup(Fields!Drug_Indication.Value,Fields!Medical_Condition.Value,Fields!Medical_Condition.Value, "ProblemList"))
I feel like I've tried everything under the sun. I'm extremely exasperated. I'm trying to get a total of each of the conditions/Indications that come from each dataset. For instance, One condition/Indication might be Addiction. There may be four addictions in from the Drug_Indication in the MedCon dataset and five addictions from the Medical_Condition in the ProblemList. I want to see Addictions 9 on the table and so and so forth for each Drug Indication/Medical Condition.
Thanks in advance for your help! Save my sanity. :) I'm sure it's probably something simple?
Tara
Thank you. I've tried using the Inmate_ID and InmateID as the key to join, but I still end up with only one of counts of either Medical_Condition or Drug_Indication.
As an example, there might be 10 addictions in one and 15 addictions in the other. I need them to be grouped under the title addiction (and whatever other titles there might be) with the total being 25.
It would look something like this.
Example Look
Something like this is close, but the counts aren't quite right.
=Count(Lookup(Fields!InmateID.Value, Fields!Inmate_ID.Value, Fields!Medical_Condition.Value, "ProblemList")) + Count(Fields!Drug_Indication.Value)
Maybe it's the way I'm grouping? How do you group on a combination of values such as Medical_condition and Drug_Indication?
Thanks again!
Tara
I think you used the Lookup() wrong. When I look at your two datasets (for me) the key to join the two datasets would be Inmate_ID.
=Lookup(Fields!InmateID.Value, Fields!Inmae_ID.Value, Fields!Medical_Condition.Value, "SecondDatasetName")
This would result in a table like this (The last column comes form the lookup above):
Drug_Name | Start_Date | Stop_Date | InmateID | Drug_Indication | Created | ID | Medical_Condition
Now you can just get the total per column:
Drug_Name | Start_Date | Stop_Date | InmateID | Drug_Indication | Created | ID | Medical_Condition
Total1 Total2
To sum Total1 and Total2 you can add a new tablix and reference to the textbox totals like this:
=ReportItems!Total1TextboxName.Value + ReportItems!Total2TextboxName.Value

what does this sql query do? SELECT column_1 FROM table_1,table_2;

SELECT column_1 FROM table_1,table_2;
When I ran this on my database it returned huge number of rows with duplicate column_1 values. I could not understand why I got these results. Please explain what this query does.
it gives you a cross product from table 1 and table 2
In more layman's terms, it means that for each record in Table A, you get every record from Table B (all possible combinations).
TableA with 3 records and Table B with 3 records gives 9 total records in the result:
TableA-1/B-1
TableA-1/B-2
TableA-1/B-3
TableA-2/B-1
TableA-2/B-2
TableA-2/B-3
TableA-3/B-1
TableA-3/B-2
TableA-3/B-3
Often used as a basis for Cartesian Queries (which themselves are the means to generate, say, a list of future dates based on a recurrence schedule: give me all possible results for the next 6 months, then restrict that set to those whose factor matches my day of the week)
This is 'valid' way of cross joining two tables; it is not the preferred way though. Cross Join would be much clearer. An on condition would then be helpful to limit results,
Imagine that i have 3 friends named Jhon, Ana, Nick; then i have in the other table 2 are T-shirts a red and a yellow and i wanna know witch is from.
So in the query being tableA:Friends and tableB:Tshirts returns:
1|JHON | t-shirt_YELLOW
2|JHON | t-shirt_RED
3|ANA | t-shirt_YELLOW
4|ANA | t-shirt_RED
5|NICK | t-shirt_YELLOW
6|NICK | t-shirt_RED
As you see this join has no relational logic between friends and Tshirts so by evaluating all the posible combination generates what you call duplicates.

one to many relationship with many columns

I am VERY new to SQL and do not know that much about it, but I am a quick learner. I have a database with Item IDs and Quantities on Hand (qoh) along with some other columns that I am not having issues with. The problem is that when I tell it to give me the item id column with the qoh column, it gives me almost 500 rows for each individual item because the qoh is different so I literally have about 12 million rows. Now, what I am looking for is the most recent quantity for each item. I am assuming that this is a one-to-many relationship and since I am below a noobie to SQL, I don't even really know where to begin. This is what I have so far:
SELECT DISTINCT item_id, qty_on_hand
FROM database.inv_mast, database.inv_loc
ORDER BY item_id ASC
Oh and I'm using Microsoft SQL Server Management Studio
Here is what I want:
| item_id |qty_on_hand|
|123456789| 93 |
|456789123| 87 |
|789456123| 74 |
etc.
But what I'm getting is:
| item_id |qty_on_hand|
|123456789| 85 |
|123456789| 82 |
|123456789| 92 |
etc.
I am getting the same item_id at least 4000 times because the database is telling me what the qoh was for literally every second of time since we've had the item. I only want what is in the warehouse at the time of me running the query. I apologize for all the noobness, but I literally don't know SQL.
You are doing a cross join by just using ",". What you need to do is a inner join I think.
SELECT DISTINCT im.item_id, qty_on_hand
FROM database.inv_mast im
INNER JOIN database.inv_loc in on im.item_id = in.item_id
ORDER BY item_id ASC
You also should look at left and right joins.
So in summary using "," is like using a cross join
You really need to learn joins to understand these queries, check out the site below.
http://www.w3schools.com/sql/sql_join.asp
The from clause FROM database.inv_mast, database.inv_loc creates the cartesian product. Narrow that down using a WHERE condition.

MySQL - Difficulty getting sum with other data between two tables

I am using the Invision Power Board suite for purchases, but they do not provide a way to get the total a customer has paid. So I am attempting to write an SQL Query for this information. My knowledge of SQL is extremely limited, so ... I may be taking the wrong approach.
From what I have gathered, the information I need is on two tables, but the way it outputs is not useful to me since it outputs every individual purchase, with only the member's unique ID (relatively useless in bulk operations and examination)
I have so far gotten to this point...
SELECT member_id, email, name,
SUM(ibf_nexus_invoices.i_total) as SUM_TOTAL
FROM ibf_members
RIGHT JOIN ibf_nexus_invoices ON ibf_members.member_id = ibf_nexus_invoices.i_member
WHERE ibf_nexus_invoices.I_status = "paid";
This takes everything from the members table and the invoices table, and outputs it like this..
------------------------------------------------------------
member_id | email | name | SUM_TOTAL |
------------------------------------------------------------
4 | email#domain.com | "Derek" | 184.22 |
------------------------------------------------------------
At first glance, this looks like what I want. But it only returns one person, not all of the people in the member's list. Is anyone versed enough with SQL to help steer me in the right direction?
You need to add
GROUP BY member_id
to the end of the query to get a row per person. Otherwise, aggregate functions like SUM() combine all the rows in the result.

mysql optimize data content: multi column or simple column hash data

I actually have a table with 30 columns. In one day this table can get around 3000 new records!
The columns datas look like :
IMG Name Phone etc..
http://www.site.com/images/image.jpg John Smith 123456789 etc..
http://www.site.com/images/image.jpg Smith John 987654321 etc..
I'm looking a way to optimize the size of the table but also the response time of the sql queries. I was thinking of doing something like :
Column1
http://www.site.com/images/image.jpg|John Smith|123456789|etc..
And then via php i would store each value into an array..
Would it be faster ?
Edit
So to take an example of the structure, let's say i have two tables :
package
package_content
Here is the structure of the table package :
id | user_id | package_name | date
Here is the structure of the table package_content :
id | package_id | content_name | content_description | content_price | content_color | etc.. > 30columns
The thing is for each package i can get up to 16rows of content. For example :
id | user_id | package_name | date
260 11 Package 260 2013-7-30 10:05:00
id | package_id | content_name | content_description | content_price | content_color | etc.. > 30columns
1 260 Content 1 Content 1 desc 58 white etc..
2 260 Content 2 Content 2 desc 75 black etc..
3 260 Content 3 Content 3 desc 32 blue etc..
etc...
Then with php i make like that
select * from package
while not EOF {
show package name, date etc..
select * from package_content where package_content.package_id = package.id and package.id = package_id
while not EOF{
show package_content name, desc, price, color etc...
}
}
Would it be faster? Definitely not. If you needed to search by Name or Phone or etc... you'd have to pull those values out of Column1 every time. You'd never be able to optimize those queries, ever.
If you want to make the table smaller it's best to look at splitting some columns off into another table. If you'd like to pursue that option, post the entire structure. But note that the number of columns doesn't affect speed that much. I mean it can, but it's way down on the list of things that will slow you down.
Finally, 3,000 rows per day is about 1 million rows per year. If the database is tolerably well designed, MySQL can handle this easily.
Addendum: partial table structures plus sample query and pseudocode added to question.
The pseudocode shows the package table being queried all at once, then matching package_content rows being queried one at a time. This is a very slow way to go about things; better to use a JOIN:
SELECT
package.id,
user_id,
package_name,
date,
package_content.*
FROM package
INNER JOIN package_content on package.id = package_content.id
WHERE whatever
ORDER BY whatever
That will speed things up right away.
If you're displaying on a web page, be sure to limit results with a WHERE clause - nobody will want to see 1,000 or 3,000 or 1,000,000 packages on a single web page :)
Finally, as I mentioned before, the number of columns isn't a huge worry for query optimization, but...
Having a really wide result row means more data has to go across the wire from MySQL to PHP, and
It isn't likely you'll be able to display 30+ columns of information on a web page without it looking terrible, especially if you're reading lots of rows.
With that in mind, you'll be better of picking specific package_content columns in your query instead of picking them all with a SELECT *.
Don't combine any columns, this is no use and might even be slower in the end.
You should use indexes on a column where you query at. I do have a website with about 30 columns where atm are around 600.000 results. If you use EXPLAIN before a query, you should see if it uses any indexes. If you got a JOIN with 2 values and a WHERE at the same table. You should make a combined index with the 3 columns, in order from JOIN -> WHERE. If you join on the same table, you should see this as a seperate index.
For example:
SELECT p.name, p.id, c.name, c2.name
FROM product p
JOIN category c ON p.cat_id=c.id
JOIN category c2 ON c.parent_id=c2.id AND name='Niels'
WHERE p.filterX='blaat'
You should have an combined index at category
parent_id,name
AND
id (probably the AI)
A index on product
cat_id
filterX
With this easy solution you can optimize queries from NOT DOABLE to 0.10 seconds, or even faster.
If you use MySQL 5.6 you should step over to INNODB because MySQL is better with optimizing JOINS and sub queries. Also MySQL will try to run them into MEMORY which will make it a lot faster aswel. Please keep in mind that backupping INNODB tables might need some extra attention.
You might also think about making MEMORY tables for super fast querieing (you do still need indexes).
You can also optimize by making integers size 4 (4 bytes, not 11 characters). And not always using VARCHAR 255.