Sql select query dealing with multiple tables - php

I have table employees with columns: id, employee_id with values say ABC1234 and 4 respectively.
Another table cashadvance with columns :id, date_advance, employee_id, amount with values say 3, 2018-06-20, 5, 1500.
Now i want do sql select query in PHP to show date_advance and amount for the specific user/logged in employee(username is employee_id e.g, ABC1234 plus pswd) .
I have trouble cause employee_id in cashadvance is the primary key for employee in table employees. Please help

Below is the query written for your purpose to get those data for employees in employees table and related data in cashadvance . Here an inner join is used , please run the query and let me know if you face any error .
$sql = "select *
from employees as 'emp'
inner join cashadvance as 'cad'
on cad.employee_id = emp.employee_id
where 1" ;

Related

Multiple PHP subqueries not giving desired result

I have a MySQL table from which I want to extract attendance information(Student Id, course/subject for attendance, date range,whether the student was present or not). I have written the following query:
SELECT
COUNT(a_id),
(
SELECT COUNT(*) FROM attendance
WHERE state = 'present'
AND `dater` BETWEEN '$a' AND '$b'
) AS Count,
stud_id
FROM attendance
WHERE
stud_id =(SELECT id FROM users WHERE NAME = '$stud')
Which is giving me the correct results, but when I change the student,its not giving me the correct count for the days recorded for present. Not mention that I have not yet added the course parameter into the query
The MySQL table is as follows:
I need help for the query to return the desired results(Count the accurate days present for each student, as well as adding the course parameter into the query so that the query will look for attendance records for a specific course, for a specific student, for a specified date range).
Looks like you want to seperate your queries:
Select (select count(*) from <database>.attendance where state = 'present' AND (dater between '$a' and '$b') AND name=(SELECT id FROM users WHERE NAME = '$stud')) as present, (select count(*) from <database>.attendance where state = 'absent' AND (dater between '$a' and '$b') AND name=(SELECT id FROM users WHERE NAME = '$stud')) as absent from <database>.attendance WHERE stud_id =(SELECT id FROM users WHERE NAME = '$stud');
try this :)
Resolved it using JOIN as follows:
SELECT u.id, a.stud_id, a.course_id, count(*) FROM attendance a
JOIN users u ON u.id=a.stud_id
JOIN courses c ON c.c_id=a.course_id
WHERE a.state='present' and dater between '2017-09-01' and '2017-09-14'
GROUP BY a.stud_id, a.course_id;
Thanks for your help.

Mysql Insert with the Select

I have two tables named employees and testdept.In employees table there are two fields as employee_id and department.testdept table contains two fields as epfno and deptid.
Now what I want to do is insert those id's from testdept table to employees table department field where employee_id matches with the epfno.
I have tried with the below code. It inserts the data as a new row. That is the problem.
INSERT INTO employees (department)
SELECT t.deptid
FROM testdept t, employees e
WHERE t.epfno = e.employee_id
If I understood you correctly, you want an UPDATE statement and not an INSERT :
UPDATE employees e
JOIN testdept t
ON(e.employee_id = t.epfno)
SET e.department = t.deptid
This will update the records in employee table with the data from the corresponding row in testdept table

how to only show some of the fields in an INNER JOIN

I have the following as my results.. the problem is I only want 1 field from the balances table and 2 fields from the userinfo table... if I replace the * with say user,avatar I get my error...
$result = mysql_query("SELECT * FROM userinfo INNER JOIN balances ON userinfo.user = balances.user ORDER By balance DESC,avatar");
if (!$result) {
die("Query to show fields from table failed");
I do not know the proper form and cant find it anywhere
TIA
John
as user column exists in both the joined tables userinfo and balances you need to prefix the table name while accessing the column
Try
SELECT userinfo.user, userinfo.avatar, balances.balance
FROM userinfo
INNER JOIN balances
ON userinfo.user = balances.user
ORDER By balance DESC,avatar

MySql - if there is no record, insert multiple rows

Before asking this question, I already search a lot of entries on Google and StockOverflow. Nothing can fulfil my question.
There are two tables - group_sale_bonuses and members. I want to check is already there records with product_id "1" in the group_sale_bonuses.
If not, I want to insert all records from members table into group_sale_bonuses with product_id "1".
My overall requirement is as follow:
IF ((Select count(id) from group_sale_bonuses where product_id = 1) = 0) THEN
INSERT INTO group_sale_bonuses (member_id, product_id, quantity_counter, credit)
SELECT id, 1, 0, 0 FROM members
END IF
But this sql causes the errors.
I know there are solutions about Insert Ignore, Where Not Exists.
But these conditions checking are based on per each record. I have thousands of records in members table. I want to make condition checking just one time like in my above sql example.
By the way, I will use this Sql in Php web application.
You could just set the code in a WHERE clause instead of the IF.
INSERT INTO group_sale_bonuses(
member_id,
product_id,
quantity_counter,
credit)
SELECT
id, 1, 0, 0 FROM members
WHERE(
SELECT
count(id) FROM group_sale_bonuses
WHERE product_id = 1
) = 0;
This should do it for all product_id's
SELECT m.product_id, m.member_id FROM members AS m
LEFT JOIN group_sale_bonuses AS gsb ON gsb.product_id = m.product_id
WHERE gsb.product_id IS NULL ;
You can filter it to a specific product_id by adding to the where clause
SELECT m.product_id, m.member_id FROM members AS m
LEFT JOIN group_sale_bonuses AS gsb ON gsb.product_id = m.product_id
WHERE gsb.product_id IS NULL AND m.product_id = 1;
Take a look at this SQLfiddle: http://sqlfiddle.com/#!2/8482c/2

Mysql select from a table and insert into another

I have a mysql table called jos_users_quizzes with the following columns:
id
quiz_id
user_id
I have a second table called jos_users with this columns
id
name
username
department
the user_id on first table is linked with the id of second table so
quiz_id = id (jos_users)
How can build a query to multiple insert the ids of a selected department into the jos_users_quizzes table... in one click
I am thinking meabe a sub query or a loop, but no sure how.
Thanks in advance!
INSERT jos_users_quizzes (quiz_id, user_id)
SELECT $quizID, id
FROM jos_users
WHERE department = 'selected department'
With INSERT ... SELECT, you can quickly insert many rows into a table from one or many tables. For example
INSERT INTO jos_users_quizzes (quiz_id)
SELECT jos_users.id
FROM jos_users WHERE jos_users.department = 100;

Categories