mysql-query

Query design in SQL - ORDER BY SUM() of field in rows which meet a certain condition

OK, so I have two tables I'm working with - project and service, simplified thus: project ------- id PK name str service ------- project_id FK for project time_start int (timestamp) time_stop int (timestamp) One-to-Many relationship. Now, I want to return (preferably with one query) a list of an arbitrary number of projects, sorted ...

Is there a way to optimize this mysql query...?

Hi Everyone... Say, I got these two tables.... Table 1 : Hotels hotel_id hotel_name 1 abc 2 xyz 3 efg Table 2 : Payments payment_id payment_date hotel_id total_amt comission p1 23-03-2010 1 100 10 p2 23...

PHP MySQL Weird Update Problem

I have a heap based table in MySQL that I am trying to update via PHP, but for some reason, the updates do not seem to be taking place. Here is my test code: <?php $freepoints[] = 1; $freepoints[] = 2; $freepoints[] = 3; foreach ($freepoints as $entrypoint) { $query = "update gates set lane='{$entrypoint}' where traffic > 50 limit 50";...

Date problem in MYSQL Query

Am looking for a query to sum values in a particular time duration say an year or a particular month in an year using MySQL syntax. Note that my transaction_date column stores daily amount transacted. Am example of a query that returns total sales in an year query would look something like this SELECT SUM(transaction_amount) WHERE tran...

how to do this in MySql Query??

ID NAME AMT 1 Name1 1000 2 Name2 500 3 Name3 3000 4 Name1 5000 5 Name2 2000 6 Name1 3000 consider above table as sample. am having a problem in my sql query, Am using like this. Select name,amt from sample where amt between 1000 and 5000 it returns all the values in the table between 1000 and 5000, instead I want to get maximum ...

SQL Joining Two or More from Table B with Common Data in Table A

NOTE: EDITED The real-world situation is a series of events that each have two or more participants (like sports teams, though there can be more than two in an event), only one of which is the host of the event. There is an Event db table for each unique event and a Participant db table with unique participants. They are joined together...

Mysql Groupby and Orderby problem

Here is my data structure when i try this sql select rec_id, customer_id, dc_number, balance from payments where customer_id='IHS050018' group by dc_number order by rec_id desc; something is wrong somewhere, idk I need rec_id customer_id dc_number balance 2 IHS050018 DC3 -1 3 IHS050018 52 ...

Can MYSQL filter by date if date is stored as text? ex "02/10/1984"

Hello! I am trying to modify an app for a client which has already a database of over 1000 items. The dates are stored as text in the database with the format "02/10/1984". The system allows you to add and remove fields to the catalog dynamically and it also allows the advanced search to have specific fields be allowed. The problem ...

Calculating Percentile Using Cumulative Data in MySQL

What should the percentile ranks be for each of these records and is there a MySQL query I can run to calculate the percentile for the score? id score cumulative_score percentile 1 100 100 ? 2 50 150 ? 3 25 175 ? 4 25 200 ? 5 ...

MySQL floating point comparison issues

I ran into an issue by introducing floating point columns in the MySQL database schema that the comparisons on floating point values don't return the correct results always. 1 - 50.12 2 - 34.57 3 - 12.75 4 - ...(rest all less than 12.00) SELECT COUNT(*) FROM `users` WHERE `points` > "12.75" This returns me "3". I have read that the ...

an issue with MySQL query

There is a weird code here that I need to make work.. can you please help me correct it . mysql_query("insert into table(column) values('$var[0]')); ...

Find products that have a list of attributes.

I'm stuck trying to solve a problem that's proving to be more difficult than it seems. Consider there is a table that associates products with attributes, it looks like this: Products_id | Attribute_id 21 | 456 21 | 231 21 | 26 22 | 456 22 | 26 22 | 11...

Can I do this in one Mysql query?

Hi I have a table with two columns: column A column B 1 2 1 2 2 1 I want to return total of ones = 3 total of twos = 3 The best I can come up with is two queries like so: SELECT sum(CASE WHEN columnA =1 THEN 1 ELSE 0 END ) + sum(CASE WHEN columnB =1 THEN 1 ELSE 0 END ) SELECT su...

SELECT DISTINCT multiple field search?

I'm trying to search multiple fields (zc_city, zc_zip and zc_state), matching against a single value input by the user. The three columns should be included in the results. Here's what I have now: $q = strtolower($_GET["q"]); if (!$q) return; $sql = "SELECT DISTINCT zc_city AS zcity FROM search_zipcodes WHERE zc_city LIKE '$q%'"; $rsd...

How to select parent row only if has at least one child?

I have a simple one-to-many relationship. I would like to select rows from the parent only when they have at least one child. So, if there are no children, then the parent row is not returned in the result set. Eg. Parent: +--+---------+ |id| text | +--+---------+ | 1| Blah | | 2| Blah2 | | 3| Blah3 | +--+---------+ Childre...

Counting the most tagged tag with MySQL

Hi folks My problem is that I'm trying to count which tag has been used most in a table of user-submitted code. But the problem is with the database structure. The current query I'm using is this: SELECT tag1, COUNT(tag1) AS counttag FROM code GROUP BY tag1 ORDER BY counttag DESC LIMIT 1 This is fine, except, it only counts the ...

MySQL: Order by field size/length

Here is a table structure (e.g. test): __________________________________________ | Field Name | Data Type | |________________|_________________________| | id | BIGINT (20) | |________________|_________________________| | ti...

Optimising (My)SQL Query

I usually use ORM instead of SQL and I am slightly out of touch on the different JOINs... SELECT `order_invoice`.* , `client`.* , `order_product`.* , SUM(product.cost) as net FROM `order_invoice` LEFT JOIN `client` ON order_invoice.client_id = client.client_id LEFT JOIN `order_product` ON order_invoice....

Getting the final value to this MySQL query...

I've got my database set up with three tables - code, tags, and code_tags for tagging posts. This will be the SQL query processed when a post is submitted. Each tag is sliced up by PHP and individually inserted using these queries. INSERT IGNORE INTO tags (tag) VALUES ('$tags[1]'); SELECT tags.id FROM tags WHERE tag = '$tags[1]' ORDER ...

Rails/mysql SUM distinct records - optimization

Hey. How would you optimize this SQL SELECT SUM(tmp.cost) FROM ( SELECT DISTINCT clients.id as client, countries.credits_cost AS cost FROM countries INNER JOIN clients ON clients.country_id = countries.id INNER JOIN clients_groups ON clients_groups.client_id=clients.id WHERE clients_groups.group_id IN (1,2,3,4,5,6,7,8,9) ...