Using the below select statement:
select
spec_sheet_color_comb.id,
spec_sheet_color_comb.pairs*2 as total
from
spec_sheet_color_comb
where
spec_sheet_color_comb.id_spec_sheet IN (4814)
And getting this result:
id total
79928 5
Now, I want to split this result according to the TOTAL quantity and get this result, 5 rows showing the ID:
Result
79928
79928
79928
79928
79928
It is really important, Thanks
Suppose you have a table having 2 columns id and total. (You can use your query instead of the table).
You need a collateral table (or view) with all the numbers 1..max_number_you_need
e.g.
(select 1 as number
union
select 2 as number
...
select 999 as number)
Then you can use the number source table joining your table you need to multiply rows
select id
from the_table t
join (table with numbers) n on n.number<=t.total
UPDATE: an example
SELECT id
from (select 1 as id, 5 as total
union
select 2 as id, 3 as total) t
join (select 1 as number
union
select 2 number
union
select 3 number
union
select 4 number
union
select 5 number) num on num.number<=t.total
order by id
Related
Need to select name,domain from table 1, than I need a sum of values from a column from table 2 with condition table1.id = table2.
Table 2 does not have the same number of columns.
I have tried joining 2 queries with UNION and UNION ALL, but i keep getting the same problem of different number of columns.
$rows = $test->query('select domain,name from customers
UNION
select SUM(customer_id) from main.rentals,main.customers where customer_id = customers.id');
Expected would be "User's Name" - "domain" - "number of rentals(integer)"
Warning: SQLite3::query(): Unable to prepare statement: 1, SELECTs to the left and right of UNION do not have the same number of result columns
If you use SUM, it will sum all ids, for example if you have a customer ID of 10 and it shows 5 times the result will be 50. In the other hand if you use count it will count the rows that that id was shown. That's why we are doing a group by. You may tweak it to fill your specific needs, but this is one way to achieve what you want.
$query = "
SELECT
domain,
name,
count(customer_id) as Total
FROM
customers
left join main.rentals on customers.id = customer_id
GROUP BY
customer_id
";
$test->query($query );
I have a system balance table "A", where the column "amount" is a debit/credit column, like this:
id amount balance
1 100 100
2 -30 70
3 40 110
4 -20 90
If a new deposit is made of i.e. $50, the new row would have to be inserted as:
5 50 140
I could achieve this by selecting last balance:
SELECT id, balance
FROM A
ORDER BY id DESC LIMIT 1
and then inserting new row:
INSERT INTO A (amount, balance)
VALUES (50, previous_balance+50)
But, is there a way to achieve this with only one query? Would it be efficient on large databases? Thank you!
The correct way to do it is :
INSERT INTO A(amount,balance)
VALUES (50, 50 +
(SELECT B.balance
FROM(SELECT balance FROM A ORDER BY id DESC LIMIT 1) AS B)
);
Because mysql can't modify the same table which you use in the SELECT part
Just combine your 2 queries:
INSERT INTO A (amount, balance)
VALUES (50, (
SELECT balance
FROM (SELECT * from A)
ORDER BY id DESC LIMIT 1
)+50)
You can use this query:
INSERT INTO A (amount, balance)
SELECT "50", SUM(balance)
FROM ( SELECT id+1 as id, 50 balance FROM A
UNION ALL
SELECT id, balance FROM A
ORDER BY id DESC LIMIT 2
) t
What Im Trying to achieve is selecting multiple rows randomly where a certain column is different from the last for example
SELECT * FROM mytable WHERE `column_for_duplicate_values` != '$thelastkey' ORDER BY RAND() LIMIT 10
the column for multiple values would hold for example:
1 , 1 , 1 , 1 , 2, 5 , 6, 6 , 6 , 6 , 6 , 11 , 11 , 11 , 19
and I want to select 1 from each. So I would get
1 , 2 , 5 , 6 , 11 , 19
The main problem with my solution may be that it's
slow
not applicable
You need to have a primary unique id in your mytable
SELECT * FROM mytable WHERE id IN (
SELECT id FROM (
SELECT id, column_for_duplicate_values
FROM mytable ORDER BY RAND()) AS rand
GROUP BY column_for_duplicate_values
ORDER BY RAND()) LIMIT 10;
The main concept is to first get a random list of all values with their id and the column you want to have unique values.
From this result we group by the value we only want to have once and only return the id. The main table will then select 10 random id's form this list.
I tried it with one of my db's. Should work with any table that has a unique id and some random other column.
If you append ORDER BY id ASC will also sort the id's.
This is my table, I should fetch the MAX (id) of each status_id.
id status_id
10 1
11 1
12 2
13 2
14 2
15 4
16 4
So, I use this sql query, it works true and fetchs me all max ID.
select status_id, max(id) as max FROM `table`
where status_id in (1,2,3,4) group by status_id
This sql command fetchs me 3 MAX id using while.
11, 14, 16....
You see, there is not any suitable id to 3rd status_id. And if there is not any suitable id to 3rd status_id just mark it as zero. So I want that sql will bring these results:
11, 14, 0, 16
You can create a subquery which basically has all the ID's you need and have a left join against it.
SELECT a.status_ID,
IFNULL(MAX(b.id), 0) maxVal
FROM
(
SELECT 1 status_ID UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4
) a
LEFT JOIN `table` b ON a.status_id = b.status_id
GROUP BY a.status_id
SQLFiddle Demo
You can join with a temporary dummy table containing all ids and a stats_id value of 0:
SELECT dummy.status_id, COALESCE(MAX(id), 0) AS max
FROM (
SELECT 1 status_id
UNION SELECT 2 status_id
UNION SELECT 3 status_id
UNION SELECT 4 status_id
) dummy
LEFT JOIN `table`
ON dummy.status_id = table.status_id
GROUP BY dummy.status_id
But this does not scale and is a maintenance nightmare (you have to change the dummy-select in case a new status_id turns up). If you have a table containing ALL status_ids, replace the dummy-select with that table.
I am using mysql and php.
I have a table with one column. I can show unique rows by:
select distinct id from id_table
This might show
1
2
3
5
What I want to do is show the number of 1's, 2's, etc there are.
I could do
select count(id) from id_table where id = 1
But then I have to run a query for every... single... row... And with hundreds of thousands of rows, not good.
Is there a way to return an array of the counts of each distinct item
Data
1, 1, 1, 2, 3, 3, 5
Results
3, 1, 2, 1
Thanks
select id, count(id)
from table
group by id
If only want count of ids, then
select count(id)
from table
group by id
Here is a simple tutorial of group by clause
http://www.w3schools.com/sql/sql_groupby.asp