How to insert data with this SQL relationship? - php

I am trying to get all records from teacher tables row teacher_id, into timetables table row teachers_id, by using right join, i tried this code but data was not inserted in timetables coloumn teacher's id.
TimeTable looks like this:
teacher_id,student_id,class_id;
Here is my query:
select timetable.* from timetable right join teachers on
timetable.teacher_id = teachers.teacher_id;
Sample data:
Teachers table:
**teacher_id | teacher_name**
1 | asa
2 | saa
3 | ddd
4 | eee
Timetable table:
**teacher_id | class_id | student_id**

As you said in question, you like to get all records data from teacher tables row.
So if you like to see all data in teachers table use this:
select teachers.* from timetable right join teachers on
timetable.teacher_id = teachers.teacher_id;
In another part you said, you can't see any data inserted in timetable, if you like to see the rows in timetable, use it :
select timetable.* from timetable right join teachers on
timetable.teacher_id = teachers.teacher_id;
and if you like to see both use it:
select timetable.*,teachers.* from timetable right join teachers on
timetable.teacher_id = teachers.teacher_id;

try below : you write same table twice that's why error occured:
select teachers.teacher_id from teachers left join timetable on
timetable.teacher_id = teachers.teacher_id;

Related

how to show a selected data in a single row?

I want to display a set of data in a single row, but I don't seem to implement it, I tried several ways, it still doesn't work. I want to display all permission names assigned for a role name in a single row.
for example :
| editor | edit-user, delete-user, delete-role |
| deleter | delete-user |
here's my sql
$datas = DB::SELECT(' SELECT roles.id, roles.name as roleName, permissions.name as PermName
FROM roles
LEFT JOIN role_has_permissions
ON (roles.id = role_has_permissions.role_id)
LEFT JOIN permissions
ON (role_has_permissions.permission_id = permissions.id)
ORDER BY roles.id');
thank you in advance :)
Can you join the two middle joins in a subquery:
LEFT JOIN (
SELECT
permissions.name as PermName
,role_has_permissions.role_id
FROM role_has_permissions
LEFT JOIN permissions ON role_has_permissions.permission_id = permissions.id)
ON roles.id = role_has_permissions.role_id

How to retrieve data with 3 tables in mysql?

I am having 2 tables :
1.internal_employee_master
id employee_name unique_id
1 Noah ABCD
2 Liam ABCD
3 William ABCD
4 Benjamin ABCD
5 Jacob EFGH
2.external_employee_master
id name unique_id
1 Elijah ABCD
2 Ethan ABCD
3 Alexander EFGH
I am using UNION query to get both tables data into single table and display this data into html table.
select id
, employee_name
, unique_id
from internal_employee_master
where unique_id = 'ABCD'
union
select id
, employee_name
, unique_id
from external_employee_master
where unique_id = 'ABCD'
I want to store payslips of both employees into single table.
I have one table payslips with emp_id and emp_type columns.
I am storing data into payslips data like:
id pay_slip emp_id emp_type
1 Noah_payslip.pdf 1 internal
2 Liam_payslip.pdf 2 internal
3 Lia_payslip.pdf 1 External
as you can see in above table i am storing emp_id and emp_type of
both the tables in single columns each.
Now, i dont undestand how to split data of internal employee and
external employee from pay_slip table and show data in html table.
Currently, i am writing below sql joins to get employee_names of
internal and external employee tables but it doesnt work for me.
$id = $_GET['id];
SELECT ps.id,ps.pdf,ps.emp_id,ps.emp_type,external_employee.name as comemp,
internal_employee.comp_empl_name as comemp
FROM pay_slip as ps
INNER JOIN internal_employee_master as internal_employee ON internal_employee.comp_trad_id = ps.trade_id
INNER JOIN external_employee_master as external_employee ON external_employee.trad_id = ps.trade_id
where ps.is_deleted = 1 AND ps.id = '".$id."'"
Please help me to join query to get name and employee_name with respect to emp_type form pay_slip table.
How about using UNION again?
SELECT
ps.id,
ps.pdf,
ps.emp_id,
ps.emp_type,
external_employee.name AS comemp,
internal_employee.comp_empl_name AS comemp
FROM
pay_slip AS ps
INNER JOIN
internal_employee_master AS internal_employee ON internal_employee.comp_trad_id = ps.trade_id
WHERE
ps.is_deleted = 1 AND ps.id = '".$id."'
AND ps.type = 'internal'
UNION ALL
SELECT
ps.id,
ps.pdf,
ps.emp_id,
ps.emp_type,
external_employee.name AS comemp,
internal_employee.comp_empl_name AS comemp
FROM
pay_slip AS ps
INNER JOIN
external_employee_master AS external_employee ON external_employee.trad_id = ps.trade_id
WHERE
ps.is_deleted = 1 AND ps.id = '".$id."'
AND ps.type = 'external'
You could try this
SELECT ps.id, ps.pay_slip, ps.emp_type, COALESCE(i.employee_name, e.name) AS name
FROM payslips ps
LEFT JOIN internal_employee_master i ON i.id = ps.emp_id AND ps.emp_type = 'internal'
LEFT JOIN external_employee_master e ON e.id = ps.emp_id AND ps.emp_type = 'External'
AND ps.id = :ID
You can see this in action here http://sqlfiddle.com/#!9/53a195/7/0
I would mention that there are a number of issues in your included tables and queries. For example, irregular column names between tables (name vs. employee_name), you've missed the is_deleted column from your example schema, and you have capitalised and non-capitalised values in the emp_type column which is confusing.

Selecting data from a table based on conditions with other tables

I'm really unsure how best to go about writing this query. I have 3 tables and I need to run a query pulling data from one, based on conditions in the others.
Tables: surveys, survey_countries, survey_categories
surveys:
id | survey_id | network_id
survey_countries:
id | survey_id | network_id | country
survey_categories:
id | survey_id | network_id | category
(The survey_id in survey_countries and survey_categories relates to the survey_id column in the surveys table as opposed to the id column).
I need to retrieve data from surveys for a specific country and a specific category. Then I need to be able to UNION for other categories, but I guess I can do that later. My attempt:
SELECT surveys.*
FROM survey_countries
LEFT JOIN surveys ON survey_countries.survey_id = surveys.survey_id ANd survey_countries.network_id = surveys.network_id
LEFT JOIN survey_categories ON survey_countries.survey_id = surveys.survey_id AND survey_categories.network_id = surveys.network_id
WHERE survey_countries.country = 'GB'
AND survey_categories.category = 'my_category'
GROUP BY surveys.id
Thanks!
EDIT: the following seems to work:
SELECT s.*, ca.category, co.country
FROM surveys s
LEFT JOIN survey_countries co ON s.survey_id = co.survey_id AND s.network_id = co.network_id
LEFT JOIN survey_categories ca ON s.survey_id = ca.survey_id AND s.network_id = ca.network_id
WHERE ca.category = 'uncategorised'
AND co.country = 'GB';
I'm just not sure it's the best way to do it since I need to grab surveys with multiple categories later on?
Without example data and an example output, helping you on this query is very difficult. However..... your 2nd join relates survey_countries with surveys. I think you mean to join it to the survey_categories. Also, you have a Group By, with no aggregate functions. Get rid of it until you have too much information an need to summarize columns
I am not saying this is correct, but it is slightly more 'correct' than yours
SELECT surveys.*
FROM survey_countries
LEFT JOIN surveys
ON survey_countries.survey_id = surveys.survey_id
AND survey_countries.network_id = surveys.network_id
LEFT JOIN survey_categories
ON survey_categories.survey_id = surveys.survey_id
AND survey_categories.network_id = surveys.network_id
WHERE survey_countries.country = 'GB'
AND survey_categories.category = 'my_category'
(If you can, load up a schema and sample data in sqlfiddle. It will make this problem easier to solve.)

Hyphen-delimited values and LEFT_JOIN

I have a row with some values hyphen-delimited:
table: live_customers
row: areas
id | areas
1 | 10-20-30
2 | 40-50-60
...
Using this...
LEFT JOIN $table5 AS table5 ON live.areas REGEXP CONCAT('(^|-) ?',table5.id,' ?($|-)')
My results looks like:
(tab id:1) area: 10
(tab id:1) area: 20
...
(tab id:2) area: 40
...
But i expect:
(tab id:1) area: 10,20,30
(tab id:2) area: 40,50,60
How could i solve that?
EDIT:
The full query looks like:
SELECT live.*,
live.id AS lid,
table1.id, table1.value AS tn_val,
table2.id, table2.value AS tp_val,
table3.id, table3.value AS ht_val,
table5.id, table5.value AS ar_val
FROM $dblist AS live
LEFT JOIN $table1 AS table1 ON live.town = table1.id
LEFT JOIN $table2 AS table2 ON live.htype = table2.id
LEFT JOIN $table3 AS table3 ON live.ht = table3.id
LEFT JOIN $table5 AS table5 ON live.areas REGEXP CONCAT('(^|-) ?',table5.id,' ?($|-)')
ORDER BY live.id ASC
PHP echoes:
...
if ($post['areas']){ // Debugging areas stuff
echo '<strong>'.$_areas.': (ar_val)</strong> '.$post['ar_val'].'<p>';
echo '<strong>'.$_areas.': (areas)</strong> '.$post['areas'].'<p>';
}
...
EDIT2:
It's quite hard for me to explain my issue in English, but i'm trying the best i can :)
in the table "live_customers" i does have this:
id | areas
1 | 10-20-30
2 | 40-50-60
...
in the table "areas" (that is a completely different table):
id | value
38 | Zone1
39 | Zone2
40 | Zone3
...
In the SQL query you see just tables variables because i previousvly declared them at the top of page:
$table5 = 'areas';
$dblist = 'live_customers';
etc..
Solution
Thanks anyone for their answers and for let me know "GROUP_CONCAT".
Here is my solution:
SELECT live.*,
live.id AS lid,
table1.id, table1.value AS tn_val,
table2.id, table2.value AS tp_val,
table3.id, table3.value AS ht_val,
table5.id, GROUP_CONCAT(table5.value) AS ar_val
FROM $dblist AS live
LEFT JOIN $table1 AS table1 ON live.town = table1.id
LEFT JOIN $table2 AS table2 ON live.htype = table2.id
LEFT JOIN $table3 AS table3 ON live.ht = table3.id
LEFT JOIN $table5 AS table5 ON FIND_IN_SET(table5.id, REPLACE(live.areas, '-', ','))
GROUP BY live.id
Result is what i expected ^^
Take it together with GROUP_CONCAT()
First thing to say is that your schema violates First Normal Form (1NF) in that the column areas is not atomic. You should not be putting 3 different values in one column.
Next you say you have a table called live_customers with a row called areas. This is nonsense. Rows do not have names, columns do. You show a bit of table with 2 columns id and areas. What table is this?
Next in the query there is no mention of a table called live_customers.
Next, if there is a column called areas in the table with the alias of live, then the output should contain that column since you are selecting live.*. That being the case, your results cannot be what you showed us, since it would contain a results column with data like 10-20-30
Finally those cannot be the results of the posted query since I can see a results column of lid specified.
If you would care to take some time over ensuring that the questionyou post makes sense, then you might get a reasonable answer.

trying to output the correct value from SQL query from comparing a different table

I'm very new with SQL and need assistance on how I can accomplish this task using the correct query.
I have 2 tables that I need to use. Table "TB1" has:
id Name
1 bob
2 blow
3 joe
table "TB2" has:
compid property
1 bob
2 blow
I am trying to get which compid is missing in "TB2" and insert it from "TB1"
the query I am doing is:
SELECT id, name from TB1, TB2 where id <> compid
what I get is 2 ouputs of Id 1, and 2, and 3 outputs from id 3. by using php:
for($i=0;$i <= mysql_num_rows($comp)-1; $i++)
{
echo mysql_result($comp, $i, 0)."<br>";
}
and I expected the ouput 3 but instead got this:
1
1
2
2
3
3
3
I understand its comparing all the rows within the table but is there a way to achieve what I am looking for?
Thanks for your time.
You are performing an implicit Cartesian JOIN which results in every row against every other row. You need to specify what attribute JOINs the two tables.
Using implicit syntax (not recommended):
SELECT id, name
FROM TB1, TB2
WHERE id <> compid
AND TB1.Name = TB2.property <-- Column join
Using explicit syntax:
SELECT id, name
FROM TB1
JOIN TB2
ON TB2.property = TB1.Name <-- Column join
WHERE id <> compid
To accomplish your goal you would need something along the lines of:
SELECT TB1.id, TB1.name
FROM TB1
LEFT JOIN TB2
ON TB2.property = TB1.Name
WHERE TB2.compid IS NULL
See it in action
It's best practice to always alias the columns you select to prevent ambiguity.
To select it you can do:
SELECT *
FROM TB1
WHERE id NOT IN (
SELECT compid
FROM TB2
);

Categories