Delete all row based on condition in MySQL - php

I want to delete rows based on condition. Let me explain in detail,
table1
-----------------
| t1id | t1detail |
|--------------- |
| 1 | iii |
| 2 | jjj |
| 3 | iik |
| 4 | jjk |
-----------------
table2
------------------------
| t2id | t1id | t1detail |
|----------------------- |
| 1 | 1 | iii |
| 2 | 1 | jja |
| 3 | 3 | iib |
| 4 | 1 | jjc |
| 5 | 2 | iid |
| 6 | 3 | jje |
| 7 | 4 | iif |
| 8 | 2 | jjg |
| 9 | 3 | iih |
| 10 | 4 | jj3 |
------------------------
Now I need to delete table1 id(t1id) at the same time in table2 all id assigned from tablet1 should be deleted.
for example, Suppose I need to delete t1id = 1 mean, it should delete from table1 and table2
in table1
1 | iii
in table2
1 | 1 | iii
2 | 1 | jja
4 | 1 | jjc
should be deleted. Kindly advice me to do in mysql
I tried with
$query = "DELETE FROM table1 INNER JOIN table2 ON table1.t1id = table2.t1id WHERE table1.t1id = {$id};
$result = mysql_query($query, $connection);
but not successfull

I'm going to assume that you don't have foreign keys setup (maybe using MyISAM storage?). If that's the case you will need to specify both tables to delete from in your query.
DELETE table1, table2
FROM table1
JOIN table2
ON table1.t1id = table2.t1id
WHERE table1.t1id = {$id}
If you had foreign keys setup you could use cascading delete, which would solve your issue as well.

Related

Query Select and Show All First Table And Second Table Even is not Exist in Second Table

SAMPLE TABLE
TABLE FIRST_TABLE
| rid | requirements |
| 1 | 2x2 pic |
| 2 | valid id |
| 3 | 137 form |
| 4 | app form |
Second table
| id | applicant_id | rid | remarks |
| 1 | 1 | 1 | pass |
| 2 | 1 | 2 | pass |
| 3 | 2 | 1 | pass |
How to select all records from first table and show even the data is not exist on second table.
Result should be like this.
applicant_id | rid | remarks |
1 | 1 | pass |
1 | 2 | pass |
1 | 3 | null |
1 | 4 | null |
this is my sample code.
select requirements from first_table
left join second_table on first_table.rid = second_table.rid
where second_table.applicant_id = 1
group by first_table.rid
//result :
applicant_id | rid | remarks |
1 | 1 | pass |
1 | 2 | pass |
You just need to move the second_table.applicant_id = 1 to the join.
select requirements, first_table.rid, remarks
from first_table
left join second_table on
first_table.rid = second_table.rid and
second_table.applicant_id = 1
group by first_table.rid
http://sqlfiddle.com/#!9/a0cffdd/17

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

SQL Query - get row exites some value on other table laravel

I have a query
$cartable = DB::table('requests')
->select('requests.*','crequest.*')
->leftJoin('crequest','crequest.id','=','requests.request_id')
->whereRaw("FIND_IN_SET('$userid', crequest.user_allows)")
->orderBy('forms_cartable.id','desc')->get();
my tables:
requests:
+------+-------------+-------------------+-------------------+
| id | request_name | request_priority | request_status |
+------+-------------+-------------------+-------------------+
| 1 | test1 | low | process |
| 2 | test2 | low | process |
| 3 | test3 | low | process |
+------+--------------+------------------+-------------------+
crequest
+------+-------------+----------------+--------------+
| id | request_id | users_allow | c_status |
+------+-------------+----------------+--------------+
| 3 | 1 | 12,13,15 | done |
| 4 | 1 | 12 | done |
| 5 | 1 | 13 | end |
| 6 | 2 | 42,12,35 | done |
| 7 | 2 | 47,65 | done |
| 8 | 3 | 42 | open |
+------+-------------+----------------+--------------+
I want if there was an existing current user ID(Logged
) in crequest.user_allows return request`s only without crequest detail
look like this:
(if current user id 12 )
request table result (after execute query)
+------+-------------+-------------------+-------------------+
| id | request_name | request_priority | request_status |
+------+-------------+-------------------+-------------------+
| 1 | test1 | low | process |
| 2 | test2 | low | process |
+------+--------------+------------------+-------------------+
but query result all record match in table crequest i need only requests rows if have match in crequest table
I don't know php but you can achieve it in MySql in so many ways. These are 3 ways among them..
Method 1:
SELECT DISINCT R.*
FROM requests R
INNER JOIN crequest CR ON R.id = CR.request_id
WHERE FIND_IN_SET ('12', CR.users_allow);
Method 2:
SELECT *
FROM requests R
WHERE EXISTS (SELECT 1 FROM crequest CR WHERE R.id = CR.request_id
AND FIND_IN_SET ('12', CR.users_allow));
Method 3:
SELECT *
FROM requests R
WHERE R.id in (SELECT request_id FROM crequest WHERE FIND_IN_SET ('12', CR.users_allow))

MySQL getting total number of enquiries for each branch

I have three tables and they are the following
User Table
+---------+-----------+--------+
| user_id | user_name | branch |
+---------+-----------+--------+
| 1 | John | 1 |
| 2 | Jim | 2 |
| 3 | Jern | 3 |
| 4 | Jack | 1 |
| 5 | Jery | 2 |
| 6 | Tom | 3 |
| 7 | Sona | 1 |
| 8 | Tina | 3 |
+---------+-----------+--------+
Branch Table
+-----------+----------------+
| branch_id | branch_name |
+-----------+----------------+
| 1 | IT |
| 2 | SALES |
| 3 | Administration |
+-----------+----------------+
Enquiry Table
+------------+---------------+---------+
| enquiry_id | enquiry_name | user_id |
+------------+---------------+---------+
| 1 | enqury_test1 | 1 |
| 2 | enqury_test2 | 2 |
| 3 | enqury_test3 | 1 |
| 4 | enqury_test4 | 3 |
| 5 | enqury_test5 | 2 |
| 6 | enqury_test6 | 5 |
| 7 | enqury_test7 | 1 |
| 8 | enqury_test8 | 2 |
| 9 | enqury_test9 | 4 |
| 10 | enqury_test10 | 6 |
| 11 | enqury_test11 | 2 |
| 12 | enqury_test12 | 7 |
+------------+---------------+---------+
From the above tables its clear that, each branch contains a number of users.
These users post multiple enquiries.
I need to get the total number of enquiries in each branch as
branch id => number of enquiries
I have tried various queries. But i couldn't get the result. Any one can help?
I am using MySQL and i need a single query to do this.
Thanks in advance
You need count and group by
select
b.branch_id,
count(e.user_id) as `total_enq`
from Branch b
left join User u on u.branch = b.branch_id
left join Enquiry e on e.user_id = u.user_id
group by b.branch_id
The query you have to perform to get you desired result is like this :-
$query = "SELECT u.branch, COUNT(u.user_id) AS `total_enquires`
FROM enquiry e INNER JOIN user u ON e.user_id = u.user_id
GROUP BY u.branch"
This will help you,and i think you don't need to join branch table as user table already contain branch_id.
This is the query
SELECT `branch`,`branch_name`,count(`user`.`user_id`),count(`enquiry_id`) FROM `user` inner join `branch` on `user`.`branch`=`branch`.`branch_id` inner join `enquiry` on `user`.`user_id`=`enquiry`.`user_id` group by `branch`
try it here
http://sqlfiddle.com/#!9/cf3eb/1
SELECT
bt.branch_id
,COUNT(enquiry_id) AS total_enquiry
FROM
enquiry_table et
INNER JOIN user_table ut on ut.user_id = et.user_id
INNER JOIN branch_table bt ON bt.branch_id = ut.branch
WHERE
1=1
GROUP BY
bt.branch_id
you can try this

Complicated mySQL query in php page

If you have seen my previous requests you will see I am a TOTAL NOOB with php and mySQL. I wish I wasn't even doing this but I have been dumped in the deep end by people that have let me down. It is 1 day away from "completion" and I am left to pick up and mend the pieces of this system I have which doesn't fully function!
I have tried to explain this as logically and clearly as possible. If you need clarification please let me know and I will do my best!
Thanks in advance for any help as I am now desperate to get this thing done!
I have 3 issues!
ISSUE1
Matching Keys
(x)
TABLE1
| C_ID | C_Eth_O (x) |
+--------------------+-------------------------+
| 234 | 8 |
| 341 | 11 |
| 440 | 2 |
TABLE2
| Eth_ID (x) | C1_Eth_O |
+-----------------------+------------------------+
| 2 | Label2 |
| 8 | Label8 |
| 9 | Label9 |
| 11 | Label11 |
I need to list all the "C1_Eth_O" values in a multi select list. The user will make multiple selections from that list. When submitted I need to get the "Eth_ID" value(s) and return all the value(s) from TABLE1 where any of the selected options "C_Eth_O" = "Eth_ID". Each "C_ID" can only have one "C_Eth_O".
ISSUE2
Matching Keys
(x)
(o)
TABLE1
| C_ID (x) |
+---------------------+
| 234 |
| 341 |
| 440 |
TABLE3
| Ail_ID (o) | Ali_Label |
+-------------------------+-------------------------+
| 1 | Label1 |
| 2 | Label2 |
| 3 | Label3 |
| 4 | Label4 |
| 5 | Label5 |
| 6 | Label6 |
TABLE4
| CA_ID | C1_ID (x) | Ail1_ID (o) |
+---------------------+------------------------+------------------------+
| 1 | 234 | 1 |
| 2 | 341 | 4 |
| 3 | 341 | 6 |
| 4 | 440 | 2 |
I need to list all the "Ali_Label" values from TABLE3 in a multi select list. The user will make multiple selections from that list. When submitted I need to get the "Ail_ID" value(s) from TABLE3 and return all the value(s) from TABLE1 where any of the selected match in TABLE4 "Ali1_ID" = "Ail_ID" & "C1_ID" = "C_ID". Each "C_ID" can have multiple "Ail_ID" values
ISSUE3
Matching Keys
(x)
(o)
(-)
TABLE1
| C_ID (x) |
+---------------------+
| 234 |
| 341 |
| 440 |
TABLE3
| Ail_ID (o) | Ali_Label |
+------------------------+-------------------------+
| 1 | Label1 |
| 2 | Label2 |
| 3 | Label3 |
| 4 | Label4 |
| 5 | Label5 |
| 6 | Label6 |
TABLE5
| R_ID | C1_ID (x) | Cf1_ID (-) |
+-----------------------+-----------------------+-----------------------+
| 1 | 234 | 768 |
| 2 | 234 | 854 |
| 3 | 234 | 768 |
| 4 | 440 | 854 |
TABLE6
| CA_ID | Cf_ID (-) | Ail1_ID (o) |
+---------------------+------------------------+------------------------+
| 1 | 768 | 1 |
| 2 | 854 | 4 |
| 3 | 768 | 6 |
| 4 | 880 | 2 |
I need to list all the "Ali_Label" values from TABLE3 in a multi select list. The user will make multiple selections from that list. When submitted I need to get the "Ail_ID" value(s) from TABLE3 and return all the value(s) from TABLE1 where any of the selected "Ali_ID" = "Ail1_ID" & "C1_ID" = "C_ID" & "Cf1_ID" = "Cf_ID". Each "C_ID" can have multiple "Cf1_ID" values AND each "Cf_ID" can have multiple "Ail1_ID" values.
I have the system working returning simple queries such as
SELECT * FROM table1 WHERE C_ID = 234
but nothing with multiple tables and multiple results per C_ID!
Thanks again!
ISSUE 1 -
SELECT * FROM table1 INNER JOIN table 2 ON table1.C_Eth_O = table2.Eth_ID
ISSUE 2 -
SELECT * FROM table1 INNER JOIN table4 ON table1.C_ID = table4.C1_ID INNER JOIN table3 ON table3.Ail_ID = table4.Ail1_ID
ISSUE 3 -
SELECT * FROM table1 INNER JOIN table5 ON table1.C_ID = table5.C1_ID INNER JOIN table6 ON table5.Cf1_ID = table6.Cf_ID INNER JOIN table3 ON table3.Ail_ID = table6.Ail1_ID

Categories