Order Matching Query - php

I have two tables, A & B
Table A Table B
order id order id quantity
----------- ------------ -------------
1 1 10
2 2 20
3 3 10
4 4 5
5 5 6
table A contains the order id and table B contains order id & quantity. I am trying
to run a MySQL query that shows rows with a total equal to or less than a number. For
example, a query to find rows equal to or less than 15 will show the rows with
order id's 4 & 5

Can you have multiple order_id and quantity pairs in table B? If so, I think what you want is this:
SELECT order_id FROM B GROUP BY order_id HAVING SUM(quantity) <= 15

If order_id is unique in table B. (does not have multiple rows with same id) then use the following:
SELECT a.order_id, col1, col2, col3
FROM TableA a
LEFT JOIN TableB b
ON a.order_id = b.order_id
WHERE b.quantity <= {$magic_number}
If order_id is not unique in table B(has multiple rows with the same order_id) then use the following:
SELECT a.order_id, col1, col2, col3
FROM TableA a
LEFT JOIN TableB b
ON a.order_id = b.order_id
GROUP BY a.order_id
HAVING SUM(b.quantity) <= {$magic_number}

Related

I am using Left join with other table . There are multiple rows with the same p_id on other table . I want the row with latest p_id [duplicate]

This question already has answers here:
Using LIMIT within GROUP BY to get N results per group?
(14 answers)
Closed 3 months ago.
I am using Left join( on A.a_id = B.p_id ) for joining tables A and B .There are multiple rows with same p_id on the other table.
I want the row with latest p_id .
The column joining table A with table B is a_id and p_id . I want to JOIN both the
table and group the records and only want table B record with max bid .
Can anyone help me with mysql query finding the desired result . I have posted the
desired result below .
Mysql query :
Select * from A Left JOIN B ON A.a_id =B.p_id group by p_id
having max(b_id)
Table A
a_id column1
1 Adam
2 Voge
Table B
b_id p_id column2
1 1 dash
2 1 Hash
3 2 kyu
Desired Result should look like this
a_id b_id column1 column2
1 2 Adam Hash
2 3 Voge kyu
#surya singh..
with joined_data as (
select
a.a_id,a.column1,b.p_id,b.column2
from tablea a
inner join tableb b on a.a_id= b.b_id
),
find_recent_data as (
select
a_id,column1,
column2,
row_number() over (partition by a_id order by p_id desc) key_column
from joined_data)
select * from find_recent_data
where key_column=1;

How to show only those sellers who have created at-least one product

I have one sellers table where sellerid column is sno and I have products table where sellerid is sellerid , I want to get those sellers who are not blocked (there is column named flag in sellers table) and have created at least one product so I write this(or maybe copy this)
SELECT e.*, count(*) AS count
FROM sellers AS e
left join products AS r ON e.sno = r.sellerid
where NOT e.flag='1'
GROUP BY e.sno
Now I do my logic through:
if($row["count"] == 1){
continue;
}
It is producing incorrect result
-- fake table data
CREATE TABLE sellers SELECT 1 sno, 0 flag UNION SELECT 2, 0 UNION SELECT 3, 0;
CREATE TABLE products SELECT 1 sellerid UNION ALL SELECT 1 UNION ALL SELECT 2;
SET sql_mode := '';
-- initial query
SELECT e.*, count(*) AS `count`
FROM sellers AS e
left join products AS r ON e.sno = r.sellerid
where NOT e.flag='1'
GROUP BY e.sno
sno
flag
count
1
0
2
2
0
1
3
0
1
-- counting joined values, not rows (NULLs are ignored)
SELECT e.*, count(r.sellerid) AS `count`
FROM sellers AS e
left join products AS r ON e.sno = r.sellerid
where NOT e.flag='1'
GROUP BY e.sno
-- further filtering by count>0 needed
sno
flag
count
1
0
2
2
0
1
3
0
0
-- join only rows which have matched rows in second table
SELECT e.*, count(*) AS `count`
FROM sellers AS e
inner join products AS r ON e.sno = r.sellerid
where NOT e.flag='1'
GROUP BY e.sno
-- no further filtering needed
sno
flag
count
1
0
2
2
0
1
db<>fiddle here

left join two tables such that two columns of table 2 does not exist in table1

I have two tables table1(userid,regid) and table2(userid,hostuserid,status) where either userid or hostuserid equals myuserid(already contained in a variable $userid).I want to find all rows in table1 such that table1.userid!=table2.userid AND table1.userid!=table2.userid.
table1
--------
userid regid
1 gbjnknnk
2 bvgcghb
3 bjbnjb
table2
-------
userid hostuserid
1 5
5 2
$userid=5
query should return only one row=> 3 bjbnjb
how to implement the same.
what i had tried
SELECT * FROM table1 JOIN table2 WHERE (table1.userid!=table2.userid AND
table2.hostuserid=$userid) AND (table1.userid!=table2.hostuserid AND
table2.userid=$userid)
You could use a not in on a subselect with union
select userid
from table1
where user1 not in (
select userid
from table2
union
select
hostuserid from table2
)

Order one table based on column of another using MySQL

I am trying to implement a join so then I can order results of one table based on the column of another table. My SQL works perfectly when the records exists in both tables. The SQL also works when there are more records in table1 than there are in table2, providing I do not use the ORDER BY clause.
SQL:
SELECT * FROM table1
JOIN table2 b ON table1.col1 = b.col1
WHERE col3 != 0 ORDER BY b.col2 ASC;
Table 1
col1 | col2 | col3
__________________
1 foo 1
2 foo 1
5 foo 1
9 foo 0
10 foo 1
17 foo 0
14 foo 1
12 foo 1
Table 2
col1 | col2
___________
1 a
2 b
17 e
14 g
12 l
The part of the query ORDER BY b.col2 ASC is causing it to fail when the records between the two tables are not matching.
I cannot guarantee that a record will be present in both. Is there a way of still implementing this?
I am currently using mysqli but can use pdo if needed.
Like #Maximus2012 mentioned, try a LEFT JOIN. This will give you all of the records from table1 and any records from table2 which match col1 from table1.
SELECT * FROM table1
LEFT JOIN table2 b ON table1.col1 = b.col1
WHERE col3 != 0 ORDER BY b.col2 ASC
If you are looking for all records from table2 and any which match from table 1, use a RIGHT JOIN instead.
SELECT * FROM table1
RIGHT JOIN table2 b ON table1.col1 = b.col1
WHERE col3 != 0 ORDER BY b.col2 ASC
Making use of a LEFT JOIN to get this query:
SELECT table1.*,
table2.col2,
CASE WHEN table2.col2 IS NOT NULL THEN '0'
ELSE '1' END AS derived_column FROM table1
LEFT JOIN table2 ON table1.col1 = table2.col1
WHERE table1.col3 <> 0
ORDER BY derived_column, table2.col2
See if the above query works for your case. It is a bit complicated since the ORDER BY clause in the original query will put the non-matching columns before the matching ones in the result. To bypass this, I have conditionally created a derived column to return a value of 0 (for matching) and 1 (for non-matching). Then you order by this derived column first (rows with value 0 will come before those with value 1) and then order by table2.col2.

adding and multiplying COUNT() of several tables

Is it possible to add and multiply the count of different tables where the id is the same?
Imagine:
Table_1 Table_2 Table_3
id id id
1 1 1
1 2 2
2 2 3
3 2 3
3 2 3
3 3 3
So that the end result would be this table with 2 columns:
id (COUNT(Table_1.id) + 2*COUNT(Table_2.id) + 3*COUNT(Table_3.id))
1 7
2 12
3 17
I don't know if I understood you correctly but give this a try,
SELECT a.ID,
a.aa + (2 * b.bb) + (3 * c.cc)
FROM
(
SELECT ID, COUNT(*) aa
FROM table1
GROUP BY ID
) a LEFT JOIN
(
SELECT ID, COUNT(*) bb
FROM table2
GROUP BY ID
) b ON a.ID = b.ID
LEFT JOIN
(
SELECT ID, COUNT(*) cc
FROM table3
GROUP BY ID
) c ON a.ID = c.ID
SQLFiddle Demo
SELECT id, counts_1.number + 2 * counts_2.number + 3 * counts_3.number
FROM
(SELECT id, COUNT(*) AS number FROM Table_1 GROUP BY id) AS counts_1
JOIN
(SELECT id, COUNT(*) AS number FROM Table_2 GROUP BY id) AS counts_2 USING (id)
JOIN
(SELECT id, COUNT(*) AS number FROM Table_3 GROUP BY id) AS counts_3 USING (id)
Note that this solution requires that every id exists at least once in each of the tables, otherwise it will be left out of the result. Changing this would require a FULL OUTER JOIN that MySQL is incapable of. There are ways around that limitation, though.

Categories