I have row and column locations of several students.
Assuming number of rows and columns are fixed (to 3x3), how can I have a query result listing all row and column combinations, with students mapped to the correct location?
For example given these students data:
Student Row Column
Paul 1 1
Chris 1 3
James 2 2
Dwayne 3 3
How to have a query output like this:
Student Row Column
Paul 1 1
NULL 1 2
Chris 1 3
NULL 2 1
James 2 2
NULL 2 3
NULL 3 1
NULL 3 2
Dwayne 3 3
Please help! Thank you very much in advance.
While using PHP, Try mysql_insert_id() for your Code.
See Example here:
http://php.net/manual/en/function.mysql-insert-id.php
Good luck.
First of all, you need to know that Mysql haven't a implicit generator of N numbers of rows, like other RDBMS have, but you can emulate this using something like this:
http://use-the-index-luke.com/blog/2011-07-30/mysql-row-generator#mysql_generator_code
Take a look for study porpuse.
But for a first approach to resolve your problem, you can try this:
SELECT IFNULL((SELECT STUDENT FROM StudentSeatPlan B WHERE B.ROW = TB.ROW_ AND B.COLUMN = TB.COLUMN_),'') AS STUDENT,
TB.ROW_,TB.COLUMN_
FROM (
SELECT 1 ROW_,1 COLUMN_ UNION ALL
SELECT 1,2 UNION ALL
SELECT 1,3 UNION ALL
SELECT 2,1 UNION ALL
SELECT 2,2 UNION ALL
SELECT 2,3 UNION ALL
SELECT 3,1 UNION ALL
SELECT 3,2 UNION ALL
SELECT 3,3) TB
Whatever, it seems like you have a schema problem, something wrong it happens that you need generate data in this form in Mysql, maybe you prefered make it in your app if is the case.
Related
I have table where the data is like bellow
Combination_id combination
1 1
1 3
1 4
2 1
2 3
2 5
I want to find the combination_id based on combination.
Test case
1 . For the combination of 1,3,4 I should get combination id 1
2 . For the combination of 1,3,5 I should get combination id 2
I tried many MySQL queries but failed. Can anyone help me here??
Thanks in advance...
There are many ways, but it depends on how your combination is used
For example
select t1.Combination_id
from tab1 t1
where t1.combination in (1,3,4)
group by t1.Combination_id
having count(1) = 3
I know this is probably so odd to ask. But lets say I have 3 tables:
Table 1
ID
Name
1
Adam
2
David
3
Conor
Table 2
ID
Name
1
Adam
2
Derek
3
Niall
Table 3
ID
Name
1
Adam
2
David
3
John
Is there any way I can write a query to get the unique names across all 3 tables. So it would return "Adam, David, Conor, Derek, Niall, John"
Order doesn't matter
If it helps, all name values are related to a names table
yes , one way is to union them
select name from table1
union
select name from table2
union
select name from table3
union automatically removes duplicate cases
I have a MySQL table which contains val_type column which have 3 type of values
id val_type company
1 rib 1
2 mod 2
3 rib 2
4 rib 3
5 mod 1
6 trop 1
$res= SELECT SUM(val_type) from tabl_name GROUP BY company;
with above query I get sum of all types in one
Result Required : Rib=3, mod=2 and trop=1
I want to get sum of all three types with one MySQL query. like how many rib,mod and trop.
Thanks
It sounds like you want to count all three types. You only need a basic GROUP BY query:
SELECT
val_type,
COUNT(*) AS cnt
FROM tabl_name
GROUP BY
val_type;
I have table like this:
id products
------ ----------
5 1,2,3
6 2,4,5
9 1,4,7
17 4,6,7
18 1,6,8
19 2,3,6
I have to select only that rows, which row's products column contains one of (2,3) values.
In this case query must return:
id products
------ ----------
5 1,2,3
6 2,4,5
19 2,3,6
But I don't understand how to make construction of this query.
Any ideas?
Thanks in advance.
SELECT id,products
FROM yourTable
WHERE FIND_IN_SET('2',products)>0
OR FIND_IN_SET('3',products)>0
sqlFiddle
Would you mind to try this one please?
select * from TABLE_NAME where products regexp "(^|,)[23](,|$)";
Its doing either two or three at the begining, or at end. Or in between the commas.
Never, never, never store multiple values in one column.
Like you see now this will only give you headaches. Normalize your table. Then you can select normally.
Your table should look like this
id product
-- -------
5 1
5 2
5 3
6 2
6 4
6 5
...
With that table structure your select would be
select id
from your_normalized_table
where product in (2,3)
group by id
having count(distinct product) = 2
That query can make use of indexes and is really fast.
I have a table that looks somewhat like this:
id value
1 0
1 1
1 2
1 0
1 1
2 2
2 1
2 1
2 0
3 0
3 2
3 0
Now for each id, I want to count the number of occurences of 0 and 1 and the number of occurences for that ID (the value can be any integer), so the end result should look something like this:
id n0 n1 total
1 2 2 5
2 1 2 4
3 2 0 3
I managed to get the first and last row with this statement:
SELECT id, COUNT(*) FROM mytable GROUP BY id;
But I'm sort of lost from here. Any pointers on how to achieve this without a huge statement?
With MySQL, you can use SUM(condition):
SELECT id, SUM(value=0) AS n0, SUM(value=1) AS n1, COUNT(*) AS total
FROM mytable
GROUP BY id
See it on sqlfiddle.
As #Zane commented above, the typical method is to use CASE expressions to perform the pivot.
SQL Server now has a PIVOT operator that you might see. DECODE() and IIF() were older approaches on Oracle and Access that you might still find lying around.