Okay guys, I will try to share my guestion understandable. So I have 2 tables like below:
ALL Id from TABLES are Autoincrement.
Number
id number
10 100
11 102
12 105
13 106
subnumber
id number_id subnumber
52 10 10
53 11 15
54 13 40
You see some numbers (not all) have subnumbers. I mean:
From table number the number where id = 13 has subnumber and it is equal to 40.
I want to save this table in another table.
$sql1=mysql_query('SELECT number FROM number');
$while(fetch1=mysql_fetch_array($sql1))
{
mysql_query('
INSERT INTO `save_number` (number)'
VALUES ('.$fetch1['number'].');
);
}
save_number
id number
100 100
101 102
102 105
103 106
Now i gonna save table (subnumber) in another table (save_subnumber)
$sql2=mysql_query('SELECT number_id, subnumber FROM subnumber');
$while(fetch2=mysql_fetch_array($sql2))
{
mysql_query('
INSERT INTO `save_subnumber` (number_id, subnumber)'
VALUES ('.$fetch2['number_id'].', '.$fetch2['number'].');
);
}
save_subnumber
id number_id subnumber
60 10 10
61 11 15
62 13 40
So, you see the number_id FROM save_subnumber is not equal to new inserted id FROM save_number. I would be appreaciated if anyone help me. By the way I am still using mysql_query. I can not find time to improve my SQL to PDO :)
Your first query can be changed to
INSERT INTO
save_number (number)
SELECT
number
FROM
number
This will save you using PHP to iterate through rows and will be faster. A lot.
Having this in mind your second query would be
INSERT INTO
save_subnumber (number_id, subnumber)
SELECT DISTINCT
sn.id, s.subnumber
FROM
saved_number AS sn
CROSS JOIN
number AS n
USING (number)
INNER JOIN
subnumber AS s
ON
n.id = s.number_id
If you just want to copy one table in another why don't you use something like this
INSERT INTO save_number SELECT * FROM number;
Related
I've to show the remaining quantity of books using eloquent query.
I can get total number of purchased books from this table
ordered_books
BOOKCODE[varchar(10)] QUNATITY[varchar(6)]
111 25
423 15
201 10
111 10
423 10
201 5
158 12
At first I've to sum the total books for each of the book code. Then from the another table I've to calculate the difference.
books_out
DISTRIBUTOR[varchar(50)] BOOKCODE[varchar(10)] QUNATITY[varchar(6)]
25 158 2
35 201 5
45 158 5
55 111 10
35 111 5
15 423 1
25 423 10
Again, from this table I've to calculate the total number of books taken by distributors then I can get the actual number of books present. How shall I write eloquent query for this scenario?
Try to use Raw query like this
==================================
$data=DB::select(DB::Raw("select t1.BOOKCODE,(total-selled) as available from (select BOOKCODE, SUM(QUNATITY) as total FROM `ordered_books` group by `BOOKCODE`) as t1,(SELECT BOOKCODE,SUM(QUNATITY) as selled FROM `books_out` group by `BOOKCODE`) as t2 where t1.BOOKCODE=t2.`BOOKCODE`"));
print_r($data);
//To get total books for each of the book code
SELECT BOOKCODE,SUM(QUNATITY) FROM `ordered_books` group by `BOOKCODE`
//To get total books_out for each of the book code
SELECT BOOKCODE,SUM(QUNATITY) FROM `books_out` group by `BOOKCODE`
Regarding your scenario, I think you require this query, please try it and comment whether it was helpful or not.
$countBooks = DB::select(DB::Raw(" SELECT A.TOTAL, IFNULL(B.SOLD,0) AS SOLD, (A.TOTAL - IFNULL(B.SOLD,0)) AS REMAINING FROM
(
select t1.BookID, SUM(t1.QUNATITY) AS TOTAL from ordered_books t1
GROUP BY t1.BookID) A
LEFT JOIN
(
SELECT BookCode, IFNULL(SUM(Quantity),0) AS SOLD FROM books_outs GROUP BY BookCode) B
ON A.BookID = B.BookCode
"));
dump($countBooks);
Also, better to display the name of books rather than code. It will be more user friendly. Thank You!!!
I have a problem trying to work out some logic when trying to sort. Even went back to Excel to work out simple logic.
What I have is :-
ColA ColB
100022 33
100022 36
100024 28
100024 32
100024 41
100024 52
100026 38
100038 28
100038 42
100038 48
100038 59
What I want to do is sort by Column B (heightest to lowest) BUT I dont want Column A to have the same values next to each other.Sorting the above will have 33 & 36 (with 100022) next to each other (and 42&48) with 100038 next to each other. As long as Column B is sorted as best it can from heightest to lowest and Column A's values are never next to each other (Unless absolutely necessary) I'm happy.
Ideally I am after a logic to end up with something like this ..
100024 28
100038 28
100022 33
100024 32
100022 36
100026 38
100038 42
100024 41
100038 48
100024 52
100038 59
Any help or ideas would be greatly appreciated.
Thank you
Brad
If you want to enforce that no two values from ColA appear adjacent, then one option would be to assign a row number to each value in a given ColA block, and then order to ensure that adjacent values do not occur.
SET #row_number = 0;
SET #ColA = NULL;
SELECT t.ColA, t.ColB
FROM
(
SELECT
#row_number:=CASE WHEN #ColA = ColA THEN #row_number + 1 ELSE 1 END AS rn,
#ColA:=ColA AS ColA,
ColB
FROM
yourTable
ORDER BY ColA, ColB DESC
) t
ORDER BY t.rn, t.ColB DESC
You mentioned in your question that wanted ColB sorted from highest to lowest, which I have done. If you want ascending ordering, just remove the DESC from the above query.
Output:
Demo here:
Rextester
Like this a result :
75 Ansari 5 10
88 Koodoo 4 0
90 Koodoo 14 0
83 Koodoo 5 0
82 Koodoo, 6 0
81 Koodoo 4 0
79 Koodoo 5 0
74 Savage 1 0
80 Strike 2 36
87 Strike 4 15
78 Sullivan 3 15
77 Sullivan 2 0
I would like to get the total for each member for the last 2 columns (Hours and Minutes).
My query look like that :
SELECT
*
FROM
$tbl_name
ORDER BY
player
If someone would have a quick fix for that I would appreciate it.
Probably you want something like this (but you didn't provide a schema so I'm guessing on the field names).
SELECT `id`, `player`, SUM(`hours`) as hours, SUM(`minutes`) as minutes FROM `$tbl_name` GROUP_BY `player`;
You need to GROUP BY the user's name and SUM the totals for the hours and minutes columns.
A Simple example of this is:
SELECT `id`,
`user`,
SUM(`hours`) as tot_hours,
SUM(`mins`) as tot_mins
FROM `test`
GROUP BY `user`;
You can see this example run at this SQLFiddle
Note: With that fiddle, I removed the extra comma in this line (I assumed it was a typo)
82 Koodoo, 6 0
If that comma is supposed to be there, you just need to adjust that one insert statement to add the comma within the quotes of the name. That will adjust your query outcome as Koodoo, won't group with the other Koodoo values.
All id are auto_increment. I will try explain my issue understandable. I just want copy 2 tables to another 2 tables. Here my tables at below:
table1
id number
10 100
11 102
12 105
13 106
table2
id number_id subnumber
52 10 10
53 11 15
54 13 40
You can see there is subnumber of some numbers. For example, WHERE id =11 from table1 has a subnumber on table2 and it is equal to 15. Now I should copy the table1 to the table : copy_table1
$sql1 = mysql_query('INSERT INTO
copy_table1 (copy_number)
SELECT
number
FROM
table1');
And the results of copy_table1 are:
copy_table1
id copy_number
100 100
101 102
102 105
103 106
Then I should copy table2 to another table copy_table2:
$sql2 = mysql_query('INSERT INTO
copy_table2 (copy_number_id, copy_subnumber)
SELECT
number_id, copy_subnumber
FROM
table2');
And the results of copy_table2 are:
id copy_number_id copy_subnumber
60 10 10
61 11 15
62 13 40
So, lets check the tables: copy_table1 and copy_table2.
You see, ID from copy_table1 is not equal and suitable to copy_number_id FROM copy_table2
And it brings me problem. How can I do that after copying tables id and copy_number_id will be suitable to each other?
$sql2=mysql_query('INSERT INTO
copy_table2 (copy_number_id, copy_subnumber)
SELECT DISTINCT
cp1.id, t2.subnumber
FROM
copy_table1 AS cp1
CROSS JOIN
table1 AS t1
USING (number)
INNER JOIN
table2 AS t2
ON
t1.id = t2.number_id ');
I want a resultset for this table:
ID Number_of_posts Number_of_user
1 100 21
2 23 34
as
ID Number_of_posts Number_of_user Number_of_posts_AND_Number_of_user
1 100 21 178
2 23 34 178
-----------------------------------------------
123 55
Is it possible to get the sum of two colums as another column/ as output in mysql?
To get cross-tab totals (horizontal and vertical):
select id,
number_of_posts as p,
number_of_users as u,
number_of_posts+number_of_users as p_and_u
from tbl
union all
select 99999 as id,
sum(number_of_posts) as p,
sum(number_of_users) as u,
sum(number_of_posts+number_of_users) as p_and_u
from tbl
order by 1
This will give you:
id p u p_and_u
----- --- --- -------
1 100 21 121
2 23 34 57
99999 123 55 178
You're complicating your query needlessly and using more memory that you have to. Pull the records in one query, then make another query to get the aggregates.
I know it doesn't answer your question, but it's what you should be doing instead. =)
SELECT id, number_of_posts, number_of_user,
(
SELECT SUM(number_of_posts + number_of_user)
FROM mytable
)
FROM mytable
SELECT SUM(Number_of_posts), SUM(Number_of_user) FROM table;
SELECT *,
(SELECT SUM(Number_of_posts) + SUM(Number_of_user) FROM TABLE) AS total
FROM table;
(Edit: Didn't originally notice it was the total total in the last column.)
Does MySQL support ROLLUP?
SELECT id,
SUM(Number_of_posts) Number_of_posts,
SUM(Number_of_user) Number_of_user,
SUM(Number_of_posts) + SUM(Number_of_user) Number_of_posts_AND_Number_of_user
FROM table
GROUP BY ROLLUP(id)
Edit: based on a quick search, in MySQL the last line might be GROUP BY id WITH ROLLUP.