Loop through every value of string - php

I get form db field (organisations.paths)the following strings:
/1/2/3/4
Each number is the id of organisation's name from this db table:
+----+-------------+----------+----------+----------------------+
| id | frameworkid | path | parentid | fullname |
+----+-------------+----------+----------+----------------------+
| 1 | 1 | /1 | 0 | NYC University |
| 2 | 1 | /2 | 0 | Board of directors |
| 3 | 1 | /1/2/3 | 1 | Math faculty |
| 4 | 1 | /1/2/3/4 | 3 | Statistic department |
| 5 | 1 | /1/2/3/5 | 2 | Linguist department |
+----+-------------+----------+----------+----------------------+
Then I have the description table for each organisation:
+----+----------+---------+----------------+
| id | data | fieldid | organisationid |
+----+----------+---------+----------------+
| 1 | HQ | 1 | 1 |
| 2 | advisory | 1 | 2 |
| 3 | advisory | 1 | 3 |
| 4 | bottom | 1 | 4 |
| 5 | advisory | 1 | 5 |
+----+----------+---------+----------------+
How to join the both description table and main table and loop only through organisations, which have HQ or advisory in their description? So it becomes:
NYC University, Board of directors, Math faculty (Statistic department-won't be shown, as it is with description bottom)

You need to use explode, IN and join Function of PHP and mysql.
$var = "/1/2/3/4";
$in = join(" , ", explode("/", ltrim($var, '/')));
$sql = "SELECT `dt`.`fullname` FROM `db_table` dt
LEFT JOIN `organization` o
ON `o`.`organisationid` = `dt`.`id`
WHERE `o`.`id` IN ($in) AND (`o`.`data` = 'HQ' OR `o`.`data` = 'advisory')";
Make a loop to get the names and show them as you want.

Related

Sales SUM per country, state, city and district from five Mysql tables

I have the five below tables, I want to show the sales SUM per country, state, city and district by using one mysql query if possible:
note: If there is no sales in the state, city, district, then I need the query result to show 0 or empty space (the state, city, district name has to be shown even if there is no sales for that specific state, city or district)
+------------+----------+
| country |
+------------+----------+
| country_id | country |
+------------+----------+
| 1 | country1 |
| 2 | country2 |
+------------+----------+
+----------------+--------+------------+
| state_province |
+----------------+--------+------------+
| state_id | state | country_id |
+----------------+--------+------------+
| 1 | state1 | 1 |
| 2 | state1 | 2 |
| 3 | state2 | 2 |
| 4 | state2 | 1 |
+----------------+--------+------------+
+---------+-------+----------+
| city |
+---------+-------+----------+
| city_id | city | state_id |
+---------+-------+----------+
| 1 | city1 | 1 |
| 2 | city2 | 1 |
| 3 | city1 | 3 |
| 4 | city2 | 3 |
| 5 | city1 | 4 |
| 6 | city2 | 4 |
+---------+-------+----------+
please note that state (state_id =2) in country (country_id = 2) has no cities in the above table.
+-------------+-----------+---------+
| district |
+-------------+-----------+---------+
| district_id | district | city_id |
+-------------+-----------+---------+
| 1 | district1 | 1 |
| 2 | district2 | 1 |
| 3 | district1 | 2 |
| 4 | district2 | 2 |
| 5 | district1 | 4 |
| 6 | district2 | 4 |
| 7 | district1 | 5 |
| 8 | district1 | 6 |
+-------------+-----------+---------+
+----------+------------+----------+---------+-------------+--------+
| sales |
+----------+------------+----------+---------+-------------+--------+
| sales_id | country_id | state_id | city_id | district_id | amount |
+----------+------------+----------+---------+-------------+--------+
| 1 | 1 | 0 | 0 | 0 | 1000 |
| 2 | 1 | 0 | 0 | 0 | 2000 |
| 3 | 1 | 1 | 0 | 0 | 300 |
| 4 | 1 | 1 | 0 | 0 | 70 |
| 5 | 1 | 1 | 1 | 0 | 50 |
| 6 | 1 | 1 | 1 | 1 | 25 |
| 7 | 1 | 4 | 1 | 1 | 25 |
| 8 | 1 | 4 | 5 | 0 | 25 |
| 9 | 2 | 0 | 0 | 0 | 3000 |
| 10 | 2 | 0 | 0 | 0 | 500 |
| 11 | 2 | 3 | 0 | 0 | 300 |
| 12 | 2 | 3 | 4 | 6 | 70 |
+----------+------------+----------+---------+-------------+--------+
Demo with my current attempt
Thank you
If you want to group by three column (city_id, state_id, district_id), it's pointless, since it won't change anything in your sales table. There you have already partitoned information.
Moreover, in sales table you have 0 in place of some IDs and there isn't any ID equal to 0 in any table. You need to rethink what you really want I guess.
I think you just need simple JOIN:
SELECT country,
state,
city,
district,
coalesce(amount, 0) amount
FROM country ctr
JOIN state_province st ON ctr.country_id = st.country_id
JOIN city c ON c.state_id = st.state_id
LEFT JOIN district d ON d.city_id = c.city_id
LEFT JOIN sales s ON
s.country_id = ctr.country_id AND
s.state_id = st.state_id AND
s.city_id = c.city_id AND
s.district_id = d.district_id
Demo

how to find displaying data where data not exist in the other table

I have 3 tables:
table_1:
| id | test |total_employee|
+----+------+--------------+
| 1 | xxxx | 3 |
| 2 | yyyy | 2 |
| 3 | zzzz | 3 |
----------------------------
table_2:
| id | id_table1 |id_employee|
+----+-----------+-----------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 1 | 3 |
| 4 | 1 | 1 |
| 5 | 1 | 2 |
| 6 | 1 | 1 |
| 7 | 1 | 2 |
| 8 | 1 | 3 |
-----------------------------
table employee:
| id | name |
+----+------+
| 1 | emp1 |
| 2 | emp2 |
| 3 | emp3 |
-------------
and now I want to display all employees from table employee and from table_2 if the employee record in table employee exists but in table_2 it does not. If record does not exist mark as "unchecked" but if exists mark as "checked".
The point is how to find data from table_employee no matter data is exist or not exist on table_2.
The IF() operation lets you do this:
SELECT e.*, IF(ISNULL(t2.id), 'unchecked', 'checked') AS `checked`
FROM table_employee e
LEFT JOIN table_2 t2 ON t2.id_employee = e.id

How to make where query with join

I have a table in which i have four columns
id
user_type
user_role
org_id
Now if the user_type is 1 (i.e individual) then the user_role and org_id will be null,
but if the user_type is 2 (i.e organisation) then it will have org_id (id of the organisation) and user_role (his role in the organisation).
There are three type of role in an organisation
admin (only one person could be the admin).
finance secrearty (could be more than one).
approvers (also could be more than one).
What I want
I want to fetch only one person (probably admin of the organisation) if the user_type is 2 from each organisation and all the users having user_type 1.
My table
-------------------------------------
| tbl_user |
-------------------------------------
| id | user_Type | user_role | org_id|
--------------------------------------
| 1 | 1 | null | null |
| 2 | 2 | 1 | 1 |
| 3 | 2 | 2 | 1 |
| 4 | 2 | 3 | 1 |
| 5 | 2 | 3 | 1 |
| 6 | 1 | null | null |
| 7 | 2 | 1 | 2 |
| 8 | 2 | 2 | 2 |
| 9 | 2 | 3 | 2 |
| 10 | 2 | 3 | 2 |
by using query
SELECT * FROM YourTable t
WHERE t.user_type = 1
OR t.user_role = 1
Expected result
-------------------------------------
| tbl_user |
-------------------------------------
| id | user_Type | user_role | org_id|
--------------------------------------
| 1 | 1 | null | null |
| 2 | 2 | 1 | 1 |
| 7 | 2 | 1 | 2 |
| 6 | 1 | null | null |
Then i have to join the result with tbl_user_meta with (where zipcode = 85225).
My tbl_user_meta follows-
-------------------------------------
| tbl_user_meta |
--------------------------------------
| id | uid | user_name | zipcode|
--------------------------------------
| 1 | 1 | abc | 85225 |
| 2 | 2 | asd | 85225 |
| 3 | 3 | asd | 85225 |
| 4 | 4 | sdf | 345678 |
| 5 | 5 | fgd | 23456 |
| 6 | 6 | rgy | 23567 |
| 7 | 7 | hyt | 12345 |
| 8 | 8 | ukl | 12345 |
| 9 | 9 | tko | 21334 |
| 10 | 10 | ert | 85225 |
What i finally want to achieve is this record
-------------------------------------
| tbl_user_meta |
--------------------------------------
| id | uid | user_name | zipcode|
--------------------------------------
| 1 | 1 | abc | 85225 |
P.S - I am doing this with codeigniter
Thanks.
you can join the table as under remember remove "" from "85225" if it is integer.
select * from tbl_user, tbl_user_meta where tbl_user.id = tbl_user_meta.id AND tbl_user_meta.zipcode = "85225";
select a.id, a.uid, b.username, a.zipcode
from tbl_user_meta a
inner join tbl_user b
where a.uid = b.id ;
This can be achieved like this in codeigniter.
public function gettable(){
$query ='select a.id, a.uid, b.username, a.zipcode
from tbl_user_meta a
inner join tbl_user b
on a.uid = b.id
where a.zipcode = 85225';
$query = $this->db->query($query);
$result = $query->result();
return $result;
}
I think I have my answer:
$this->db->select('tbl_user.*,tbl_user_meta.*');
$this->db->from('tbl_user');
$this->db->where('user_type', 1);
$this->db->or_where('user_role', 1);
$this->db->where('tbl_user_meta.zipcode',85225);
$this->db->join('tbl_user_meta','tbl_user_meta.did=tbl_user.id');
$query=$this->db->get();
return $query->result();

Treat data from mysql for html

I want to treat data from mysql for display in HTML, using PHP.
I have three database tables: student, course, student_x_course
student:
| idStudent | firstname | surname |
-----------------------------------
| 1 | John | Regular |
| 2 | John | Smith |
| 3 | Claire | White |
course:
| idCourse | coursename |
--------------------------
| 1 | Art |
| 2 | Music |
| 3 | Math |
| 3 | Biology |
student_x_course:
| idsc | idStudent | idCourse |
-------------------------------
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 1 | 3 |
| 4 | 2 | 1 |
| 5 | 2 | 2 |
| 6 | 2 | 4 |
| 7 | 3 | 1 |
| 8 | 3 | 2 |
| 9 | 3 | 3 |
| 10 | 3 | 4 |
And I want to create an html table which looks like this:
| Art | Music | Math | Biology |
------------------------------------------------
John Regular | x | x | x | - |
John Smith | x | x | - | x |
Claire White | x | x | x | x |
My sql query is:
SELECT s.firstname, s.surname, c.coursename FROM student AS s INNER JOIN student_x_course AS sxc ON (s.idStudent = sxc.idStudent) INNER JOIN course ON (c.idCourse = sxc.idCourse);
which gets me the following:
| John | Regular | Art |
| John | Regular | Music |
| John | Regular | Math |
| John | Smith | Art |
| John | Regular | Music |
| John | Smith | Biology |
| Claire | White | Art |
| Claire | White | Music |
| Claire | White | Math |
| Claire | White | Biology |
My question is: How can I get from many rows to lines?
Is there a better sql query or do I have to handle this in PHP Code?
Any suggestions?
You are trying to pivot your results. In mysql, you can do this with conditional aggregation:
SELECT s.idStudent, s.firstname, s.surname,
max(case when c.coursename = 'Art' then 'x' end) Art,
max(case when c.coursename = 'Music' then 'x' end) Music,
max(case when c.coursename = 'Math' then 'x' end) Math,
max(case when c.coursename = 'Biologoy' then 'x' end) Biologoy
FROM student AS s
INNER JOIN student_x_course AS sxc ON (s.idStudent = sxc.idStudent)
INNER JOIN course ON (c.idCourse = sxc.idCourse)
GROUP BY s.idStudent

SQL statement to show difference between every user choice and one user choice

Basically I need to create output TOP table where users are arranged by comparing their points with admin's points.
For example:
User3 | 0 //Everything was as admin had.
User5 | 3 //One song had 2 points different from admin and one was off by one
ect.
In my database I have three tables:
Table: rating
+------------+---------+----------+---------+
| rating_id | user_id | song_id | points |
+------------+---------+----------+---------+
| 1 | 1 | 4 | 0 |
| 2 | 1 | 3 | 1 |
| 3 | 3 | 2 | 3 |
| 4 | 4 | 2 | 2 |
| 5 | 2 | 1 | 4 |
Table: songs
+---------------+------------+
| song_name_id | song_name |
+---------------+------------+
| 1 | Song1 |
| 2 | Song2 |
| 3 | Song3 |
| 4 | Song4 |
| 5 | Song5 |
Table: users
+----------+----------+----------+
| id | username | password |
+----------+----------+----------+
| 1 | User1 | passw |
| 2 | User2 | wordp |
| 3 | User3 | somet |
| 4 | User4 | hings |
It should be something like this (not in any programming language):
Compare user_id > 1 with user_id=1 //Let's say that the comparable admin is user_id=1
$result= ABS(user.points-admin.points)++;
And put this to array as:
username => result
Then when I sort this array by result, I can print it as top table - who got the closest result to admin!
I tryed several different solutions but never got the right result.
Can anybody help me?
UPDATE:
Thanks!
With JOIN the result is:
+------------+---------+----------+---------+-----------+
| song_id |song_name| user_id |username |rating_diff|
+------------+---------+----------+---------+-----------+
| 1 | Song1 | 1 | admin | 0 |
| 2 | Song2 | 1 | admin | 0 |
...etc...
With LEFT JOIN the result is:
+------------+---------+----------+---------+-----------+
| song_id |song_name| user_id |username |rating_diff|
+------------+---------+----------+---------+-----------+
| 1 | Song1 | 11 | user2 | NULL |
| 1 | Song1 | 10 | user1 | NULL |
| 1 | Song1 | 12 | user3 | NULL |
| 1 | Song1 | 1 | admin | 0 |
| 2 | Song2 | 11 | user2 | NULL |
| 2 | Song2 | 10 | user1 | NULL |
| 2 | Song2 | 12 | user3 | NULL |
| 2 | Song2 | 1 | admin | 0 |
..etc..
So.. Something is wrong, the rating_diff does not work.
Assuming you want this comparison on a song-by-song basis (instead of a total or average across all songs), try:
select r.song_id,
s.song_name,
r.user_id,
u.username,
abs(r.points - r1.points) rating_diff
from rating r
join songs s on r.song_id = s.song_name_id
join users u on r.user_id = u.id
join rating r1 on r.song_id = r1.song_id and r1.user_id = 1
order by s.song_name, abs(r.points - r1.points)
This should sort the output by the song name, and then by the difference between the admin's points and the users' points. (Change the join on rating r1 to be a left join if you can't guarantee an admin rating for every song.)

Categories