Data output in a single row by order of importance - mysql

I'm trying to get data that is on multiple rows into a single row by order of importance.
I was working with multiple tables and was able to pull all the data I need into one table - so currently I'm working with one table where the data I need exists in multiple rows. Example a person can have more than one role. However, the roles have an order of importance - I added an order of importance column to the file I'm working with.
The file I'm working with looks like this:
ID | FIRST |LAST | ROLE | ORDER OF IMPORTANCE
116 | Jamie | Ansto | PARAL | 5
116 | Jamie | Ansto | FMREMP | 11
153 | Alan | Rond | PAR | 3
153 | Alan | Rond | PARAL | 5
155 | Maureen | Aron | GP | 4
155 | Maureen | Aron | PARAL | 5
38 | William | Dry | STU | 8
175 | Nathan |Gong | OTH | 10
175 |Nathan |Gong | FMRSTU | 13
175 |Nathan | Gon | FR | 14
308 | Bridget | Abad | PAR | 3
308 | Bridget | Abad | EMP | 7
370 | Matt | Bodie | BD | 1
370 | Matt | Bodie | AL | 2
What I need is a file that has all the codes associated with one person on the same row in the order of their importance.
I want to end up with something that looks like this:
ID |FIRST |LAST |CODE1 |CODE2 |CODE3 |CODE4
116 |Jamie |Ansto |PARAL |FMREMP
153 |Alan |Rond |PAR |PARAL
155 |Maureen |Aron | GP | PARAL
381 |William |Dry |STU
175 |Nathan |Gong |OTH |FMRSTU |FR
308 | Bridget |Abad |PAR |EMP
370 | Matt |Bodie |BD | AL
I tried using Group_Concat but it didn't give me the results in the order I wanted. Any help would be appreciated.
Thanks,
MG

You can do something like this:
SELECT *,GROUP_CONCAT(`ROLE` ORDER BY `ORDER_OF_IMPORTANCE` SEPARATOR ' ' )
FROM `table1` GROUP BY `ID`;
The SEPARATOR ' ' function will give you result like this OTH FMRSTU FR. If you remove it and only do GROUP_CONCAT(ROLE ORDER BY ORDER_OF_IMPORTANCE), the result will look like this OTH,FMRSTU,FR instead.

Related

how do i insert number series which start with 100 in mysql php?

I was trying to insert number in my database which should be start with 100. following is my query which i tried to insert in my database but it is showing every time with one not by 101.
INSERT INTO jps_final_tickets(ft_ticket_number, ft_event, ft_package_id, ft_contact_person)
SELECT COUNT(ft_ticket_number)+1, '$eventID' AS ft_event, '$ticketID' AS ft_package_id, '$contactPerson' AS ft_contact_person,
FROM jps_final_tickets WHERE ft_package_id = '$ticketID'
Above query showing following output, everything is fine but ticket number should be start from 100 series.
-------------------------------------------------
| Ticket Number | event ID | Ticket Id | Name |
| 1 | 645 | 70 | Santosh |
| 2 | 645 | 70 | Sandeep |
| 1 | 645 | 71 | Sahil |
| 1 | 645 | 72 | Jagveer |
--------------------------------------------------
Following output which i want:
-------------------------------------------------
| Ticket Number | event ID | Ticket Id | Name |
| 101 | 645 | 70 | Santosh |
| 102 | 645 | 70 | Sandeep |
| 101 | 645 | 71 | Sahil |
| 101 | 645 | 72 | Jagveer |
--------------------------------------------------
Please help me how can i insert above ticket number in my table.
Just increment the counter by 101.
SELECT COUNT(ft_ticket_number) + 101
Detailed explanation:
Initially there are no records for let's say ticket Id 70
COUNT(ft_ticket_number) will return 0 for ticket Id 70. So first value will be 0 + 101 = 101
Next time table already has 1 record. So COUNT(ft_ticket_number) will return 1 which will calculate Ticket Number as 1 + 101 = 102 and so on.
The counter will reset automatically for new Ticket Id.
This would be appropriate for you..
INSERT INTO jps_final_tickets(ft_ticket_number, ft_event, ft_package_id, ft_contact_person)
SELECT COUNT(ft_ticket_number)+101, '$eventID' AS ft_event, '$ticketID' AS ft_package_id, '$contactPerson' AS ft_contact_person,
FROM jps_final_tickets WHERE ft_package_id = '$ticketID'
What about telling it mysql directly. If you make ticket_number an auto increment value starting at 100.
If jps_ticket_number is your primary key this will work.
ALTER TABLE jps_final_tickets AUTO_INCREMENT=100;

Get Number of A's in Result Table - MySQL

This is the case. In my school all classes prepare excel sheet for each class with marks for each subject in term end test. There are 17 classes. I combine them in to access table. Then again export all data in to excel. make csv file . And import to Mysql Database using phpmyadmin. now I have result table as follow.
| ID | Name | Religion | Sinhala | science | english | maths | History | Categery 1 | Categery 2 | Categery 3 | Total | Average | Rank | |
|---- |------- |---------- |--------- |--------- |--------- |------- |--------- |------------ |------------ |------------ |------- |--------- |------ |--- |
| 1 | manoj | 45 | 65 | 78 | 98 | 67 | 67 | 63 | 76 | 64 | 654 | 62 | 12 | |
Sectional Head Need to get number of students who got >75 for all Subject.
And Number of Student Who got >75 for 8 subject out of 9.
I need to retrieve number of A s, B s (marks >=75) from this table.
Ex. Student names and Number of A s
Total Number of A for all 9 subject - 45
Total Number of A for all 8 subject (any 8 subject ) - 45
Total Number of A for all 7 subject (any 7 subject ) - 45
I Tried following SQL Statement
SELECT COUNT(SELECT COUNT()
FROM result
WHERE religion >=75
AND Math >=75)
FROM result
I read about same scenario in stack overflow.
Access 2010
this one get some point. but I cant solve it for my scenario.
Use GROUP BY studentName and SUM(grade = 'A') AS numberOfAs.
[Quick answer bc question is quickly formatted]

mysql table having a->b and b->a values, select only a->b set of values

I have one table having 5 columns
linkid, orinodeno, orinodeno, ternodeno, terifindex
linkid is autoincremented. orinodeno, oriifindex is one combination value and ternodeno, terifindex other combination (orinodeno,oriifindex is originating value and ternodeno,terifindex terminating value i.e, in between there is a link eg just like map two pts n in between connecting link) so my table contains a->b values (i.e a is combination of orinodeno, oriifindex and b is combination of ternodeno,terifindex) and b->a values. so I have to select only a->b set of values not b->a. Also sending my table image. My Table
There is no a map definition in sql databases, forget it. Check any database normalization tutorial. Then you shouldn't have any problems with select statements.
Please be clear about what you are asking. If you can not explain in words, please give example input and your expected output.
From link of table image you have provided and description, It looks like you expect following:
Data in current table:
------------------------------------------------------------------
|linkid | orinodenumber | oriifindex | ternodenumber | terifindex|
------------------------------------------------------------------
|305 | 261 | 2 | 309 | 2 |
|306 | 309 | 2 | 261 | 2 |
|307 | 257 | 10 | 310 | 10 |
|308 | 310 | 10 | 257 | 10 |
|309 | 257 | 11 | 310 | 11 |
------------------------------------------------------------------
Expected Output:
------------------------------------------------------------------
|linkid | orinodenumber | oriifindex | ternodenumber | terifindex|
------------------------------------------------------------------
|305 | 261 | 2 | 309 | 2 |
|307 | 257 | 10 | 310 | 10 |
------------------------------------------------------------------
If that is your case, following query might help you (Assuming table name as link_table):
SELECT *
FROM link_table o
WHERE EXISTS (SELECT linkid
FROM link_table i
WHERE o.orinodenumber = i.ternodenumber
AND o.oriifindex = i.terifindex
AND o.linkid < i.linkid);

SQL select default value when there is no such value

I have following tables in DB.
ACCOUNT TABLE
User_id| first_name | last_name | age |
_______|_____________|____________|_________|
1 | LeBron | James | 28 |
2 | Kobe | Bryent | 29 |
3 | Kevin | Durant | 30 |
4 | Jim | Jones | 31 |
5 | Paul | Pierce | 32 |
6 | Jeremy | Lin | 33 |
USER_BOOKMARK TABLE
User_id| Bookmarked_user_id
_______|____________________
1 | 2
1 | 3
1 | 4
2 | 1
2 | 4
3 | 1
5 | 6
I want to select user's information from ACCOUNT table and also whether that person is in my Bookmark list
ex) Lebron James wants to know Jeremy Lin's information and whether Jeremy is in he's bookmark lists.
Desired results =>
User_id| first_name | last_name | age | isBookmarked |
_______|_____________|____________|_________|______________|
6 | Jeremy | Lin | 33 | 0 | =>0 means no.
*It must return only one row.
*If user is on my bookmark list, value of isBookmarked is my user_id.
What I tried =>
SELECT ACCOUNT.user_id, ACCOUNT.firstname, ACCOUNT.lastname, coalesce(User_Bookmark.user_id, 0) as isBookmarked
FROM Account LEFT OUTER JOIN User_Bookmark ON Account.user_id = User_Bookmark.Bookmarked_user_id
WHERE Account.user_id=6 AND User_Bookmark.user_id=1
But this query returns zero rows... since I'm not an expert on sql, I assume that I'm missing something. Can anyone help me?
The User_Bookmark.user_id = 1 test is filtering out the non-matching rows, because that column will be NULL when there's no match. When doing a LEFT JOIN, you have to put conditions on the second table into the ON clause rather than WHEN.
SELECT ACCOUNT.user_id, ACCOUNT.firstname, ACCOUNT.lastname, coalesce(User_Bookmark.user_id, 0) as isBookmarked
FROM Account
LEFT OUTER JOIN User_Bookmark
ON Account.user_id = User_Bookmark.Bookmarked_user_id AND User_Bookmark.user_id=1
WHERE Account.user_id=6

MySQL Count within an IF

+-------------+--------------+----------+-------+
| ticketRefNo | nameOnTicket | boughtBy | event |
+-------------+--------------+----------+-------+
| 38 | J XXXXXXXXX | 2 | 13 |
| 39 | C YYYYYYY | 1 | 13 |
| 40 | M ZZZZZZZZZZ | 3 | 14 |
| 41 | C AAAAAAA | 3 | 15 |
| 42 | D BBBBBB | 3 | 16 |
| 43 | A CCCCC | 3 | 17 |
+-------------+--------------+----------+-------+
+-------------+------------------+--------------+---------------------+--------+
| ticketRefNo | cardNo | cardHolder | exp | issuer |
+-------------+------------------+--------------+---------------------+--------+
| 38 | 4444111133332222 | J McKenny | 2016-01-01 00:00:00 | BOS |
| 39 | 4434111133332222 | C Dempsey | 2016-04-01 00:00:00 | BOS |
| 40 | 4244111133332222 | M Gunn-Davis | 2018-02-01 00:00:00 | RBS |
+-------------+------------------+--------------+---------------------+--------+
+-------------+-------------+----------+
| ticketRefNo | boxOfficeID | paidWith |
+-------------+-------------+----------+
| 41 | 1 | card |
| 42 | 2 | cash |
| 43 | 3 | chequ |
+-------------+-------------+----------+
I have a database with the data shown above. It represents a ticket-buying system. I would like to be able to see a list of tickets bought with the name of the event and either the boxOfficeID or the issuer of the debit card.
I have tried running the following code, to no avail.
SELECT t.ticketRefNo AS 'Reference', t.event AS 'Event',
IF(COUNT(SELECT * FROM Online WHERE t.ticketRefNo=o.ticketRefNo;) >= 1,
o.issuer, InPerson.boxOfficeID) AS 'Card Issuer or Box Office'
FROM Ticket AS t, InPerson, Online AS o
WHERE t.ticketRefNo=o.ticketRefNo;
Cheers in advance!
Some notes: the semicolon character isn't valid syntax; if you have a need to delimit the subquery, wrap it in parens. Escape column aliases like you'd escape any other identifier: use backticks, not single quotes. Single quotes are used around string literals.
Assuming that issuer in the Online table is NOT NULL, and assuming that ticketRefNo is unique in both the Online and InPerson tables, you could do something like this:
SELECT t.ticketRefNo AS `Reference`
, t.event AS `Event`
, IF(o.ticketRefNo IS NOT NULL,o.issuer,i.boxOfficeId)
AS `Card Issuer or Box Office`
FROM Ticket t
LEFT
JOIN InPerson i
ON i.ticketRefNo = t.ticketRefNo
LEFT
JOIN Online o
ON o.ticketRefNo = t.ticketRefNo
Use outer join operations to find matching rows in the InPerson and Online tables, and use a conditional test to see if you got a matching row from the Online table. A NULL will be returned if there wasn't a matching row found.
It's not a good idea to have one column JOINing to two different tables with some values in each of the two tables.
But here goes anyway:
( SELECT ... FROM Ticket t JOIN InPerson x USING(ticketRefNo) ... )
UNION ALL
( SELECT ... FROM Ticket t JOIN Online x USING(ticketRefNo) ... )
ORDER BY ...
The ALL assumes that InPerson and Online never have any overlapping ticketRefNos.
The ORDER BY an the end is in case you want to sort things, although I see no need for it in your attempted SELECT.
The two SELECTs must have the same number of columns.