mysql query error matching data - php

I'm getting an error in sql
I have three tables
T1, t2, t3
in t1
id name
1 a
2 b
3 c
4 d
5 e
in t2
t1_id name
4 sac
2 sau
4 rah
4 seh
1 kaif
5 zah
6 aas
8 ram
in t3
t1_t2_id name count lif_lin
1 Eve 2 no
2 sun 1 no
3 mon 0 no
4 tue 3 no
5 wed 1 no
6 thu 1 no
I want to count the how much t1_id element's exist in t1 respectively of id, t1_id in t1_t2_id
Mean I have category id in t1 and the same category id in t2 table with t1_id and t3 t1_t2_id and i want to count the how many matches found in t2 with the same t1_t2_id

Not sure I exactly understand what you are saying but try:
SELECT count(a.id)
from t1 a
join t2 b on a.id = b.t1_id
join t3 c on a.id = c.t1_t2_id

I think this will work for you (number if t1 ids in t2):
SELECT count(*) from t1,t3 where t1.id=t2.t1_id;

Related

mysql get last 2 records of table with 3 tables

I have 3 mysql tables (order , camp , user) as bellow values,
order_table
ID camp_id orderDate message
1 1 2015-01-01 ok
2 1 2015-02-01 ok
3 2 2015-03-01 not ok
4 3 2015-01-01 not ok
5 1 2015-04-01 not ok
6 2 2015-01-01 ok
7 3 2015-07-01 not ok
camp_table
camp_id camp_uid camp name
1 10 first camp
2 11 second camp
3 12 third camp
4 10 forth camp
user_table
uid uname
10 abc
11 xyz
12 wrt
i want to have result as bellow
uname,camp name,message
for last 2 records of each user from order_table for today order by orderDate
I want to join these tables to have uname from user_table and camp name from camp_table and message from order_table.
for today order by orderDate
Thanks
Select
ct.camp_name,
ut.uname,
ot.message FROM order_table as ot
LEFT JOIN camp_table as ct on ot.camp_id = ct.camp_id
LEFT JOIN user_table as ut on ct.camp_uid = ut.uid
order by ot.id desc
limit 2
SELECT
u.uname,
ct.camp_name,
ot.message
FROM
(
SELECT
*
FROM
order_table o1
WHERE
(
SELECT
COUNT(*)
FROM
order_table o2
WHERE
o1.camp_id = o2.camp_id
AND o2.ID >= o1.ID
) <= 2
) ot
INNER JOIN camp_table ct ON ct.camp_id = ot.camp_id
INNER JOIN user_table u ON ct.camp_uid = u.uid
ORDER BY
u.uname
Order By Date and join both table.
Select t1.camp_name, t2.uname,t3.message FROM order_table as t3
LEFT JOIN camp_table as t1 on t3.camp_id = t1.camp_id
LEFT JOIN user_table as t2 on t1.camp_uid = t2.uid
order by t3.orderDate desc
limit 2

How to join this 5 tables in mysql?

I have 5 tables that I need to connect to get the necessary data.
Table1
id | number
1 | 1
2 | 5
Table 2
id | number | user_id
1 | 1 | 9
2 | 5 | 8
Table 3
id | name |
8 | john |
9 | jane |
Table 4
id | email
6 | johndoe#example.com
Table 5
id | table4_id | table3_id
1 | 6 | 8
Table 1 is my main table and I want to add the name and email from table 3 and 4 respectively to my select query of table 1, but in order to do so, I would need to use table 2 and 5 to connect them as there is no direct relationship between table 1 and table 3 and 4. I only know how to join 3 tables and not 5 tables and it seems confusing to me on how to proceed with this.
I followed the link here to join Table 1,2 and 3. But I don't know how to proceed with table 4 and 5.
This is the query I tried:
SELECT table1.number, table2.number, table2.user_id, table3.id, table3.name,
table4.id, table4.email, table5.table4_id, table3_id
FROM table1
LEFT JOIN table2
INNER JOIN table3
//this query will work if I don't include this 2 inner joins
INNER JOIN table4
INNER JOIN table5
ON table3.id = table5.table3_id
ON table5.table4_id = table4.id
//
ON table2.user_id= table3.id
ON table2.number = table1.number;
ERROR: (if included the inner join for table 4 and 5)
Error Code: 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 'ON table2.user_id= table3.id
The right Syntax is select .. from .. join .. on .. join ..on ....
SELECT table1.number, table2.number, table2.user_id, table3.id, table3.name, table4.id, table4.email, table5.table4_id, table3_id
FROM table1
LEFT JOIN table2 ON table2.number = table1.number
INNER JOIN table3 ON table2.user_id= table3.id
INNER JOIN table5 ON table3.id = table5.table3_id
INNER JOIN table4 ON table5.table4_id = table4.id
Try with the below structure. I made it from the structure you given in question
select t1.number, t2.number, t2.user_id, t3.id, t3.name,t4.id, t4.email, t5.t4_id, t3_id
from table5 as t5
join table4 as t4 on t5.table4_id = t4.id
join table3 as t3 on t5.table3_id=t3.id
join table2 as t2 on t2.user_id = t3.id
join table1 as t1 on t2.number=t1.number
Try Below code. Hope this will work.
Select t3.name,t4.email,t1.number,t2.user_id
from table3 t3 JOIN table5 t5 ON t3.id=t5.table3_id
JOIN table4 t4 ON t4.id=t5.table4_id
JOIN table2 t2 ON t2.user_id=t3.id
JOIN table1 t1 ON t1.number=t2.number;

How to update single rows in duplicate entries

I have two tables t1 and t2
t1->
id line Amt
----------- ----------- -----------
1 1 0
1 2 0
2 1 0
2 2 0
2 3 0
3 3 0
3 4 0
3 5 0
4 2 0
4 3 0
--------------------------
t2->
id amt
----------- -----------
1 500
2 350
3 750
4 400
In this case I need to update t1 with amount from t2. But I need to update only one row for each id on minimum line. I can do it in MSSQL using the following query-
update a set a.amt=c.amt from T1 a inner join (
select id,min(line) line from T1 group by Id) b
on a.id=b.id and a.line=b.line
Inner join T2 c on a.id=c.Id
I want to do it in MYSQL. Is there any idea to do something like this
You need to tweak your syntax remove from clause, move set clause after joins part
update T1 a
inner join (
select id,min(line) line from T1 group by Id
) b on a.id=b.id and a.line=b.line
inner join T2 c on a.id=c.Id
set a.amt=c.amt
DEMO

Pull recrods which is not available in another table codeigniter

I am looking for query result where I can see only Table 1 data which is not in Table 2; Here is my table definition and data;
Table 1
id name father name age
1 a a father 60
2 b b father 70
3 c c father 60
4 d d father 50
5 e e father 20
6 f f father 32
7 g g father 40
Table 2
id account_amount
1 42
3 90
5 80
7 49
Now I want all those records from Table 1 which is either not available in Table 2 or its correspondence account_amount in Table 2 is less than 50. Here would be the required outout
id name father name age
1 a a father 60
2 b b father 70
4 d d father 50
6 f f father 32
7 g g father 40
Thanks in advance for solution query in codeigniter
With anti join:
SELECT t1.id, t1.name, t1.father_name, t1.age
FROM table1 t1
LEFT JOIN table2 t2
ON t1.id = t2.id AND t2.account_amount >= 50
WHERE t2.id IS NULL
With not exists:
SELECT t1.id, t1.name, t1.father_name, t1.age
FROM table1 t1
WHERE NOT EXISTS
(SELECT 1 FROM table2 t2 WHERE t2.id = t1.id AND t2.account_amount >= 50)
Since you tagged the question with MySQL the first option gives a little gain in performance.
In the model
$this->db->query("SELECT t1.id, t1.name, t1.father_name, t1.age
FROM table1 t1
LEFT JOIN table2 t2
ON t1.id = t2.id AND t2.account_amount >= 50
WHERE t2.id IS NULL");
Imo Active Record is inconvenient for this kind of queries
Here you are:
$this->db->select('id');
$excludedData = $this->db->get('table2')->result_array();
foreach($excludedData as $key => $record){
$excludedData[$key] = $record['id'];
}
$this->db->where_not_in('id',$excludedData);
$desiredData = $this->db->get('table1')->result_array();

MYSQL/PHP sum with group by

tableA
id B_id C_id
1 1 0
2 1 0
3 0 1
4 0 2
5 0 2
6 0 2
tableB
id amount quantity
1 10 2
tableC
id amount quantity
1 6 1
2 15 3
I have this kind of database and I know it is not structured very well because I only continued this website and I wasn't given much time to restructure the website.
My question is how can i get the total amount of tableB and tableC using a LEFT JOIN of the two tables to tableA. As you can see the quantity in tableB and tableC will make the same number of tableA record. I was able to get each of the transactions amount using this code:
SELECT *
FROM tableA A
LEFT JOIN tableB B ON A.id = B.id
LEFT JOIN tableC C ON C.id = A.id
GROUP BY B.id, C.id
It would return:
id B_id C_id B.id B.amount B. quantity C.id C.amount C.quantity
1 1 0 1 10 2 0 0 0
3 0 1 0 0 0 1 6 1
4 0 2 0 0 0 2 15 3
but now I want to get the total using mysql SUM but GROUP BY cannot be used with the aggregate function SUM so the total should be: 10+6+15 = 31
Is this what you want?
SELECT coalesce(sum(b.quantity), 0) + coalesce(sum(c.quantity), 0)
FROM tableA A LEFT JOIN
tableB B
ON A.id = B.id LEFT JOIN
tableC C
ON C.id = A.id;
This returns the total across the two tables.
EDIT:
If you just want the sum of tables b and c, what is table a for? Is this what you want?
select amountb + amountc
from (select sum(amount) as amountb from tableb) b cross join
(select sum(amount) as amountc from tablec) c;

Categories