I am facing one problem with selecting data from mysql and I cant figure it out. Try to do research but without luck.
I have table with 4 columns:
id | name | price | discount_price
1 | Test 1 | 150 | 50
4 | test 2 | 130 | 300
2 | test 3 | 200 | 0
3 | test 4 | 130 | 10
4 | test 5 | 80 | 0
And I need to select data and order them by price and if discount_price is not "ZERO", than order by price_discount. Issue is, that now it is not working as I expected. It shows results as first column is sorted and in the end is second column sorted. Depends if I choose ASC or DESC.
I need to achieve something like merge this two columns into one and than sort. Like this:
id | name | price | discount_price
3 | test 4 | 130 | 10*
4 | test 5 | 80* | 0
1 | Test 1 | 150 | 50*
2 | test 3 | 200* | 0
4 | test 2 | 130 | 300*
Is it possible in mysql? Or should sort this afterwards in php ? Thank you for helping.
Try this (the field definitions in the table creation are pseudocode):
CREATE TEMPORARY TABLE sort (id INT, name VARCHAR, INT price, INT discount_price, INT actual_price) ;
INSERT INTO sort (id, name, price, discount_price)
SELECT id, name, price, discount_price
FROM original_table ;
UPDATE sort SET actual_price = price WHERE discount_price = 0 ;
UPDATE sort SET actual_price = discount_price WHERE discount_price != 0 ;
SELECT id, name, price, discount_price FROM sort ORDER BY actual_price ;
Related
How to query for erase the view below?
+-------------------+------------+
| Order_id | Weight |
| 20 | 4 |
| 21 | 5 |
| 22 | 2 |
| 22 | 2 |
+-------------------+------------+
To be like this:
+-------------------+------------+
| Order_id | Weight |
| 20 | 4 |
| 21 | 5 |
| 22 | 2 |
| 22 | |
+-------------------+------------+
When displaying results but not entered into the database.
A simple way is:
select DISTINCT order_id, weight from xyz
UNION
select order_id, null from xyz
group by order_id, weight
having count(*) > 1
Order by weight desc;
The 1st select statement will display all the unique values and 2nd one will retrieve only the repeated values.
In your required output table, it seems like you want to display all the non-repeated rows and the 1st column value of repeated rows but not 2nd column value. The above query will allow you to do that.
OK, here is how to do it:
SELECT
Order_id,
Weight,
if(#order_id = Order_id, '', Weight) as no_dup_weight,
#order_id := Order_id as dummy
FROM Table1
ORDER BY Order_id asc;
You basically need to check to see if the previous Order_id is the same as the current, and if they are, output an empty field.
Here is an SQLFiddle demonstrating the solution.
Do you actually need 2 rows for the dupes? Can't you just use the DISTINCT clause as per http://www.mysqltutorial.org/mysql-distinct.aspx
Or is it important to know what has duplicates. In which case you should look into the GROUP BY clause
I have been working on a product database schema and one of the requirements is that in order to consider a product as discontinued all of the product skus must be set to discontinued.
Product Table:
id
slug
name
One Product to Many ProductSkus
ProductSku Table
id
slug
name
product_id
product_sku_availability_id
One ProductSku to One ProductSkuAvailability
ProductSkuAvaliability
id
slug
name
now I don't really need an exact query, but is there a way to only select products where all productSkus.product_sku_availability are discontinued? here is a sample of what I am asking:
| productId | productSkuId | availabilityId |
| 1 | 1 | 1 |
| 1 | 2 | 1 |
| 2 | 3 | 2 |
| 2 | 4 | 1 |
in the above sample I would only want to select the productId = 1 because both product skus have an availabilityId of 1.
If the value of the column availabilityId has got the same value for every productId then are the maximum and minimum value equal. Because of that should a
GROUP BY productId
with
HAVING (MIN(availabilityId) = 1 AND MAX(availabilityId) = 1)
produce the desired result.
i have a table temporary as follow as:
student | Data | number
-----------|---------------|--------------
1 | book | 2
1 | book | 5
1 | book | 9
2 | book | 1
2 | book | 5
i will show reduction of column in like as output column as follow as:
student | Data | number |output (number column of next row-previous line )
-----------|---------------|----------------|--------------
1 | book | 2 | 0
1 | book | 5 | 3 (result of (5-2=3)
1 | book | 9 | 4 (result of (9-5=4)
2 | book | 1 | 0
2 | book | 5 | 4 (result of (5-1=4)
how are writing of php's script is correct? because i'm confused
You didn't mention your DBMS, so this is standard SQL:
select student,
data,
number,
number - lag(number,1,number) over (partition by student order by id) as output
from the_table
order by student, id
SQLFiddle example
The following script will subtract the number from previous number for the same student. Here's how you can do it in MySQL (which doesn't support window functions.)
SELECT
t1.student,
t1.Data,
t1.number,
IF (t2.number IS NULL, 0, t1.number - MAX(t2.number)) as output
FROM
tbl t1
LEFT JOIN
tbl t2
ON
t1.student = t2.student
AND t1.number > t2.number
GROUP BY
t1.student, t1.Data, t1.number
Here's the SQL Fiddle
Background
I have a MySQL table for which each record represents a region- and/or platform-specific version of an item. For any given item, there will be several versions; there's no primary key and mostly indexed columns.
I start with worldwide records, one for each platform-version of the item. Then I add records for any region-specific values, then add records for any country-specific values. The thing is that I only plan to add values that are unique to that region or country; in other words, all records are going to have null values because I don't want to enter repeated values, so I want records to inherit values from other records.
item | platform | region | country | date | price | [...]
1 | 1 | [WW] | null | 2013-04-01 | 100 |
1 | 2 | [WW] | null | 2013-04-01 | 100 |
1 | null | [EU] | null | 2013-04-20 | 80 |
1 | null | [UK] | null | null | 70 |
I plan to use PHP to display the relevant records for a given country. The thing is, I want to be able to combine/inherit values from that country's region record and the worldwide record. So the UK would have two total records: each one inheriting a platform value from the [WW] record, both inheriting the date value from [EU] record, and both having the price value from the [UK] record.
1 | 1 | [UK] | 2013-04-20 | 70
1 | 2 | [UK] | 2013-04-20 | 70
The question I want to know is there a solution/procedure/method of doing it in MySQL only? Or is the only way to do it is via PHP coding?
What you've requested
Please note this is NOT a real answer. It only outputs what you've asked in the question, but the logic here barely makes any sense so it is highly unlikely to be applicable for a real database.
SELECT a.item, b.platform, a.region, a.country, c.date, a.price FROM
(SELECT item, region, country, price FROM table WHERE platform IS NULL AND date IS NULL GROUP BY item) AS a
LEFT JOIN
(SELECT platform FROM table WHERE platform IS NOT NULL) AS b
ON a.item = b.item
LEFT JOIN
(SELECT date FROM table WHERE PLATFORM IS NULL AND date IS NOT NULL) AS c
ON a.item = c.item
Better Answer Here
A more organized and perhaps easier way (and still efficient if you don't go up more than 2 layers of parents) would be:
id | parent_id | item | platform | region | country | date | price | [...]
1 | null | 1 | 1 | [WW] | null | 2013-04-01 | 100 |
2 | null | 1 | 2 | [WW] | null | 2013-04-01 | 100 |
3 | 1 | 1 | null | [EU] | null | 2013-04-20 | 80 |
4 | 2 | 1 | null | [UK] | null | null | 70 |
SELECT items.*,
parent_items.platform AS pa_platform, parent_items.region AS pa_region, parent_items.country AS pa_country, parent_items.date AS pa_date, parent_items.price AS pa_price,
grandparent_items.platform AS gpa_platform, grandparent_items.region AS gpa_region, parent_items.country AS gpa_country, parent_items.date AS gpa_date, parent_items.price AS gpa_price
FROM items
LEFT JOIN
items AS parent_items
ON items.parent_id = parent_items.id
LEFT JOIN
items AS grandparent_items
ON parent_items.parent_id = grandparent_items.id
Then you have the choice of either using app level logic to display the closest non-empty value:
$region = $result['region'] ? $result['region'] : ($result['pa_region'] ? $result['pa_region'] : $result['gpa_region']);
or you can modify the above SQL to chose the first non-null value:
SELECT COALESCE(items.region, parent_items.region, grandparent.region) AS region, COALESCE(items.platform, parent_items.platform, grandparent.platform) AS platform, ...
Now... If you are actually going to add rows with dependencies
Why not simply make different tables?
Suppose you'll have a price for each region, each platform, each country, and you know the order of precedence (let's say as an example region > country > platform):
Why not make a base table (tbl_platform) with fields id/item/platform/date/price
then a country table (tbl_country) with fields id/platform_id/date/price
then a region table (tbl_region) with fields id/country_id/date/price
If you want the base info, just grab it directly from the base table, and if you want the region info, join the region to the country, then to the base.
i have a mysql table i.e
st_id | name | email | maths | chemistry | bio | social_study
1 | john |#a.com | 20 | 23 | 10 | 15
my question is how can i find the highest subject score, the second last and so on
Note that all the subject fields have int(11) values
Break your database into 3 tables like:
Students:
st_id | name | email
1 | john |#a.com
Courses:
cr_id | name
1 | maths
2 | chemistry
3 | bio
4 | social_studies
StudentCourses:
st_id | cr_id | score
1 | 1 | 20
1 | 2 | 23
1 | 3 | 10
1 | 4 | 15
Now you can do:
SELECT s.name, MAX(sc.score) FROM Students s INNER JOIN StudentCourses sc ON s.st_id = sc.st_id;
SELECT * FROM <table>
ORDER BY <field> DESC
LIMIT <needed number of rows>
Example:
SELECT * FROM <table>
ORDER BY maths+chemistry+bio+social_study DESC
LIMIT 3
Strictly PHP method: I assume you want to maintain association with field names. In that case, just use asort($row); on each row in your query result, assuming you fetched the row as an array. asort will sort the array from lowest value to highest (with additional flags to tweak the results if needed), while maintaining keys. A foreach loop will then allow you to work with each key/value pair in the sorted order.
st_id | name | email | maths | chemistry | bio | social_study
1 | john |#a.com | 20 | 23 | 10 | 15
The query can be for top marks
SELECT id,GREATEST(mark,mark1,mark2) AS `top` FROM `students`