Currently I am working on Codeigniter projected related to stores management. In model I used the following function to get the purchase & issues summary for the view through controller.
Function
function issueDetailReport($id,$start,$end){
$this->db->select('*, store_update_stock_details.item,
CASE
WHEN store_update_stock.order_status = "purchase" THEN tbl_supplier.supplier_name
WHEN store_update_stock.order_status = "issue" THEN store_officer.officer_name
END AS supplier');
$this->db->from('store_update_stock');
$this->db->join('store_update_stock_details','store_update_stock.update_stock_id=store_update_stock_details.update_stock_id');
$this->db->join('store_officer','store_update_stock.supplier=store_officer.officer_id');
$this->db->join('tbl_supplier','store_update_stock.supplier=tbl_supplier.supplier_id');
$this->db->join('store_item','store_update_stock_details.item=store_item.item_id');
$this->db->where("store_update_stock.status='1' and store_item.item_id=$id");
//$this->db->where('store_update_stock.update_stock_id in (select update_stock_id from store_update_stock) ');
if($start!=NULL && $end!=NULL)
$this->db->where("store_update_stock.billed_date BETWEEN '$start' AND '$end'");
$this->db->order_by('store_update_stock.purchased_date','DESC');
$q=$this->db->get();
if($q->num_rows()>0){
return $q->result();
}
return false;
}
02) All are working fine. But the case constructor fires the following error
A Database Error Occurred
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.order_status = "issue" THEN store_officer.officer_name END AS supplier FR' at line 1
03) I tried to solve the issue. But did't get the desired output. I can't find the error & what may be wrong. Can anyone help me ?
Supplier Table
+-------------+---------------+
| supplier_id | supplier_name |
+-------------+---------------+
| 500 | ABC |
| 501 | DEF |
| 502 | GHI |
| 503 | JKL |
+-------------+---------------+
officer Table
+------------+--------------+
| officer_id | officer_name |
+------------+--------------+
| 1000 | Danial |
| 1001 | Jhon |
| 1002 | William |
| 1003 | Patrick |
| 1004 | Salman |
+------------+--------------+
Output
+------+--------------------------+------------+------------+--------------+
| item | supplier / officer_name | start | end | order_status |
+------+--------------------------+------------+------------+--------------+
| A4 | ABC | 2018-11-01 | 2018-11-01 | purchase |
| A5 | DEF | 2018-11-01 | 2018-11-01 | purchase |
| A3 | Danial | 2018-11-02 | 2018-11-02 | issue |
| B5 | Jhon | 2018-11-05 | 2018-11-05 | issue |
+------+--------------------------+------------+------------+--------------+
Try changing:
$this->db->select('*, store_update_stock_details.item,
CASE
WHEN store_update_stock.order_status = "purchase" THEN tbl_supplier.supplier_name
WHEN store_update_stock.order_status = "issue" THEN store_officer.officer_name
END AS supplier');
To:
$this->db->select('store_update_stock_details.item,
CASE
WHEN store_update_stock.order_status = "purchase" THEN tbl_supplier.supplier_name
WHEN store_update_stock.order_status = "issue" THEN store_officer.officer_name
END AS supplier, *');
MySQL doesn't like stuff in the select statements after *'s and many times it'll refuse to work
Related
I'am working on attendance project. In that i am storing students who are absent on the particular date in the table 'attendance' with fields -> sno,rollno,subject_code,date. And if all the students are present then i will store subject_code, date and rollno as NULL which means'all students are present'
------|--------|------------|-----------|
sno |rollno |subject_code| date |
1 | 1234 | a110 | 12-12-2012|
2 | 1235 | a110 | 12-12-2012|
3 | 1235 | a111 | 14-12-2012|
iam taking the students details from table 'students' with fields -> rollno,name
|--------|-------|
| rollno | name |
| 1234 | xyz |
| 1235 | abc |
| 1236 | mno |
| 1237 | qrs |
Now I want to list the students attendance such that output should be like this for particular date assume list for the date "12-12-2012"
list of "12-12-2012" for subject a110 list of "14-12-2012" for subject a111
|-------|------|---------| |-------|------|---------|
|rollno | name | status | |rollno | name | status |
| 1234 | xyz | Absent | | 1234 | xyz | Present |
| 1235 | abc | Absent | | 1235 | abc | Absent |
| 1236 | mno | Present | | 1236 | mno | Present |
| 1237 | qrs | Present | | 1237 | qrs | Present |
Please Help me..
Use LEFT JOIN to find out which student doesnt have a row in the attendance table. (those where present)
SELECT s.rollno,
s.name,
CASE WHEN a.rollno IS NULL THEN 'Present'
ELSE 'Absent'
END as status
FROM student s
LEFT JOIN attendance a
ON s.rollno = a.rollno
i have trouble with mysql, i dont find the way to do it maybe i dont know the good mysql keyword
mysql5
+----------+------------+----------+
| ID | FOREIGNKEY | TRAINER |
+----------+------------+----------+
| ... | ... | ... |
| 475 | 254 | NULL |
| 476 | 254 | NULL |
| 477 | 254 | NULL |
| 478 | 286 | NULL |
| 479 | 286 | FREE |
| 480 | 286 | FREE |
| 481 | 401 | FREE |
| 482 | 401 | 1 |
| 483 | 401 | FREE |
| 484 | 405 | NULL |
| 485 | 405 | 1 |
| 486 | 405 | 5 |
| 487 | 405 | FREE |
| 488 | 406 | 1 |
| 489 | 406 | 5 |
| 490 | 406 | 5 |
| 491 | 406 | 2 |
| ... | ... | ... |
+----------+------------+----------+
Expected result
Constraint :
i would like to get all the foreignkey id that have not all trainer NULL or FREE (at least 1 but can be 2 or more) but at least one should be NULL
+------------+-------+
| ID_TR | FIELD |
+------------+-------+
| 405 | .. |
+------------+-------+
i dont know how to do it in mysql ?
Group then HAVING one trainer == FREE OR NULL ?
thanks for helping me
This sounds like a classic usecase for the EXISTS operator:
SELECT *
FROM mytable a
WHERE EXISTS (SELECT 1
FROM mytable b
WHERE a.foreignkey = b.foreignkey
AND trainer IS NOT NULL
AND trainer <> 'FREE'
EDIT:
If you just just want the distinct different foreignkeys:
SELECT DISTINCT foreignkey
FROM mytable a
WHERE EXISTS (SELECT 1
FROM mytable b
WHERE a.foreignkey = b.foreignkey
AND trainer IS NOT NULL
AND trainer <> 'FREE'
SELECT t.*
FROM my_table t
JOIN cross_table x ON x.FOREIGNKEY = t.ID_TR
WHERE x.TRAINER IS NOT NULL
AND x.TRAINER <> 'FREE'
GROUP BY t.ID_TR
You can count the number that are 'FREE' and NULL and then do logic:
select foreignkey,
sum(trainer is null) as NumNulls,
sum(trainer = 'Free') as NumFrees,
count(*) as Num
from table t
group by foreignkey
You then want to add a having clause to get what you want. I am not sure exactly what this means: "i would like to get all the foreignkey id that have not all trainer NULL or FREE (at least 1 but can be 2 or more) but at least one should be NULL".
For instance, this might be what you want:
having NumNulls > 0 and NumFrees > 0
or perhaps this:
having NumNulls > 0 and NumFrees > 0 and cnt >= 2;
I'm using CASE to clean up some state abbreviations, in a table, but it's working contrary to the logic. I selected the length alone to show that the length is being calculated correctly, so I think it's the CASE logic that's off
When I query...
SELECT billing_state,
length(billing_state),
CASE billing_state
WHEN length(billing_state) > 2 THEN (select state_abbr from lkup_states where upper(state_name) = billing_state)
WHEN length(billing_state) = 2 THEN upper(billing_state)
ELSE 'UNKNOWN'
END as billing_state_fixed
FROM accounts
+---------------+-----------------------+---------------------+
| billing_state | length(billing_state) | billing_state_fixed |
+---------------+-----------------------+---------------------+
| GA | 2 | NULL |
| Alabama | 7 | ALABAMA |
| MS | 2 | NULL |
| FL | 2 | NULL |
| NULL | NULL | UNKNOWN |
+---------------+-----------------------+---------------------+
However, when I enter this bizarro logic, it works.
SELECT billing_state,
length(billing_state),
CASE billing_state
WHEN length(billing_state) = 2 THEN (select state_abbr from lkup_states where upper(state_name) = billing_state)
WHEN length(billing_state) <> 2 THEN upper(billing_state)
ELSE 'UNKNOWN'
END as billing_state_fixed
FROM accounts
+---------------+-----------------------+---------------------+
| billing_state | length(billing_state) | billing_state_fixed |
+---------------+-----------------------+---------------------+
| GA | 2 | GA |
| Alabama | 7 | AL |
| MS | 2 | MS |
| FL | 2 | FL |
| NULL | NULL | UNKNOWN |
+---------------+-----------------------+---------------------+
Can anyone take a swing at this one?
Per the docs, your syntax isn't quite correct.
You've muddle CASE value WHEN compare_value and CASE WHEN expression.
What you probably want is:
SELECT billing_state,
length(billing_state),
CASE
WHEN length(billing_state) > 2 THEN (select state_abbr from lkup_states where upper(state_name) = billing_state)
WHEN length(billing_state) = 2 THEN upper(billing_state)
ELSE 'UNKNOWN'
END as billing_state_fixed
FROM accounts
I'm trying to create a data abstraction in SQL where I can choose the quarter I want to look at for my employee info. Right now I'm using the naming system Q1_P1_N for "Quarter 1 Project 1 name" and Q1_P1_W is "...project 1 weight." Employees can work on multiple projects.
So far what I have is:
CREATE PROCEDURE effort_lookup4(IN proj_name VARCHAR(20), IN quarter INT(1))
BEGIN
SET #Qx_P1_N = CONCAT('Q', quarter, '_P1_N');
SET #Qx_P2_N = CONCAT('Q', quarter, '_P2_N');
SET #Qx_P1_W = CONCAT('Q', quarter, '_P1_W');
SET #Qx_P2_W = CONCAT('Q', quarter, '_P2_W');
SET #var1 = (SELECT sum(#Qx_P1_W) FROM table_test WHERE #Qx_P1_N = proj_name);
SET #var2 = (SELECT sum(#Qx_P2_W) FROM table_test WHERE #Qx_P2_N = proj_name);
My problem is that whenever I call a query with #Qx_P1_N or #Qx_P1_W I'm not actually passing the correct query in and I can't figure out what I'm doing wrong. This should be pretty easy I'm just new to using SQL.
Here's an example of what the table looks like, except it carries on into Q2_P1_N and so on through the quarters:
+------+---------+---------+---------+---------+
| EMPID| Q1_P1_N | Q1_P2_N | Q1_P1_W | Q1_P2_W |
+------+---------+---------+---------+---------+
| 1000 | ProjA | ProjB | 0.50 | 0.50 |
| 1001 | ProjA | NULL | 1.00 | NULL |
| 1010 | ProjB | NULL | 1.00 | NULL |
| 1011 | ProjA | ProjB | 0.50 | 0.50 |
+------+---------+---------+---------+---------+
Thanks
To do what you want with your existing data structure requires that you use prepared statements (you build your desired SELECT statement as a string that you then PREPARE and EXECUTE).
However, you will probably find it easier to change your data structure:
+------+---------+---------+--------+
| EMPID| quarter | project | weight |
+------+---------+---------+--------+
| 1000 | 1 | A | 0.50 |
| 1000 | 1 | B | 0.50 |
| 1001 | 1 | A | 1.00 |
| 1010 | 1 | B | 1.00 |
| 1011 | 1 | A | 0.50 |
| 1011 | 1 | B | 0.50 |
+------+---------+---------+--------+
On your select statement you have a variable in the field that should be the column name in the table. You will need to switch the #Qx_P1_W for Q1_P1_W.
I'm working with a MySQL database and I've run into two problems. The first is trying to add a row to an existing table using the SET system. Here's the code and the error message I received:
mysql> INSERT INTO Instructors
-> SET FacId = 96
-> SET FirstName = 'Chris'
-> SET LastName = 'Explorer'
-> SET HomePhone = '555-1492';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right syntax to use
near 'SET FirstName = 'Chris' SET LastName = 'Explorer' SET
HomePhone = '555-1492'' at line 3
The Second problem is a a search for all phone numbers that don't follow the preset pattern of ###-11##. Here, again, is the code and the error I received:
mysql> SELECT FacId, FirstName, LastName, WorkPhone
-> FROM Instructors
-> WHERE WorkPhone != '555-11__';
+-------+-----------+------------+-----------+
| FacId | FirstName | LastName | WorkPhone |
+-------+-----------+------------+-----------+
| 5 | Carlson | Detroit | 555-1196 |
| 12 | Victoria | Windsor | 555-5874 |
| 20 | George | Herman | 555-1147 |
| 31 | Justin | Morgan | 555-5874 |
| 33 | Thomas | OHara | 555-3698 |
| 34 | Thett | Poker | 555-4865 |
| 35 | David | Goliath | 555-1178 |
| 37 | Dennis | Locker | 555-1169 |
| 43 | Sean | Pent | 555-4874 |
| 44 | Elizabeth | Tallman | 555-5588 |
| 48 | Jane | Fonder | 555-1122 |
| 49 | Joe | Footballer | 555-6611 |
| 83 | Allan | German | 555-5533 |
| 84 | Gregg | Packer | 555-5874 |
+-------+-----------+------------+-----------+
14 rows in set (0.00 sec)
I'm at a loss as to what I'm doing wrong with these. Any ideas?
You use SET for UPDATE commands, not INSERT commands. Try this instead:
INSERT INTO Instructors (FacId, FirstName, LastName, HomePhone)
VALUES (96,'Chris','Explorer','555-1492');
And for your second question (which should actually be a second question):
SELECT FacId, FirstName, LastName, WorkPhone
FROM Instructors
WHERE WorkPhone NOT LIKE '555-11__';
You need to look up the syntax for INSERT, it goes like this:
INSERT INTO TableName (ColumnName1, ColumnName2. .. )
VALUES (Value1, Value2. . .)
You want the NOT LIKE operator, not the != operator.