Select from 6 tables get one result only - php

I have about 6 tables and I am while looping on them the results comes 1 result it should be more
see my code here
$institute = $_POST['institute'];
$sections = $_POST['sections'];
$division = $_POST['division'];
$getSearch = $db->prepare("SELECT
a.name, a.phase, a.setNumber, a.email, a.sudImage, a.activity, a.id AS stud_id,
b.id, b.ins_name,
c.id, c.sec_name,
d.id, d.div_name,
e.id, e.std_id, e.sub_id, e.absence,
f.id, f.sub_name, f.subHour, f.level
FROM student_basic_info AS a
JOIN institutes AS b ON (a.institute = b.id)
CROSS JOIN ins_sections AS c ON (a.section = c.id)
CROSS JOIN ins_division AS d ON (a.division = d.id)
CROSS JOIN student_absence AS e ON (a.id = e.std_id)
CROSS JOIN ins_subjects AS f ON (e.sub_id = f.id)
WHERE a.institute =? AND a.section = ? AND a.division =?
GROUP BY a.id
");
$studSearch = array();
$getSearch->bind_param('iii', $institute, $sections, $division);
if ($getSearch->execute()) {
$results = $getSearch->get_result();
while ($vStud = mysqli_fetch_assoc($results)) {
$studSearch[] = $vStud;
var_dump($studSearch);
?>
the var_dump
array (size=1)
0 =>
array (size=17)
'name' => string 'الدفع النقدي' (length=23)
'phase' => string 'ابتدائي' (length=14)
'setNumber' => int 0
'email' => string 'johnef' (length=6)
'sudImage' => string '' (length=0)
'activity' => int 0
'stud_id' => int 7
'id' => int 3
'ins_name' => string 'معهد الفنون' (length=21)
'sec_name' => string 'فنون مسرح' (length=17)
'div_name' => string 'شعبة مرجانية 10' (length=26)
'std_id' => int 7
'sub_id' => int 3
'absence' => string '' (length=0)
'sub_name' => string 'فنون تطبيقية' (length=23)
'subHour' => string '2' (length=1)
'level' => string 'المستوي الأول' (length=25)
EDIT
SELECT a.name, a.phase, a.setNumber, a.email, a.sudImage, a.activity,
a.id AS stud_id, b.id, b.ins_name, c.id, c.sec_name, d.id, d.div_name,
e.id, e.std_id, e.sub_id, e.absence, f.id, f.sub_name, f.subHour, f.level
FROM student_basic_info AS a
JOIN institutes AS b ON (a.institute = b.id)
CROSS JOIN ins_sections AS c ON (a.section = c.id)
CROSS JOIN ins_division AS d ON (a.division = d.id)
CROSS JOIN student_absence AS e ON (a.id = e.std_id)
CROSS JOIN ins_subjects AS f ON (e.sub_id = f.id)
WHERE a.institute =1 AND a.section = 1 AND a.division =7
GROUP BY a.id
and yes I got the same results (1) record cam out which it should be at last (6) or (7) of records

Related

selecting from 6 tables useing JOIN got duplicated value

her is the mySql to select the students
$institute = $_POST['institute'];
$sections = $_POST['sections'];
$division = $_POST['division'];
$getSearch = $db->prepare("SELECT
a.name, a.phase, a.setNumber, a.email, a.sudImage, a.activity, a.id AS stud_id,
b.id, b.ins_name,
c.id, c.sec_name,
d.id, d.div_name,
e.id, e.std_id, e.sub_id, e.absence,
f.id, f.sub_name, f.subHour, f.level
FROM student_basic_info AS a
CROSS JOIN institutes AS b ON (a.institute = b.id)
CROSS JOIN ins_sections AS c ON (a.section = c.id)
CROSS JOIN ins_division AS d ON (a.division = d.id)
CROSS JOIN student_absence AS e
CROSS JOIN ins_subjects AS f ON (e.sub_id = f.id)
WHERE a.institute =? AND a.section = ? AND a.division =?
GROUP BY a.id
");
$studSearch = array();
$getSearch->bind_param('iii', $institute, $sections, $division);
if ($getSearch->execute()) {
$results = $getSearch->get_result();
//var_dump($studSearch);
$row_cnt = $results->num_rows;
?>
The Problem is
the results should have 2 rows effected
row one come's out right the second come's duplicated
see the var_dump row one
array (size=1)
0 =>
array (size=17)
'name' => string 'Yousef' (length=6)
'phase' => string 'اعدادي' (length=12)
'setNumber' => int 1234
'email' => string 'johnef' (length=6)
'sudImage' => string '' (length=0)
'activity' => int 0
'stud_id' => int 2
'id' => int 3
'ins_name' => string 'fonon' (length=5)
'sec_name' => string 'فنون مسرح' (length=17)
'div_name' => string 'شعبة مرجانية 10' (length=26)
'std_id' => int 4
'sub_id' => int 3
'absence' => string '1' (length=1)
'sub_name' => string 'فنون تطبيقية' (length=23)
'subHour' => string '2' (length=1)
'level' => string 'المستوي الأول' (length=25)
see the var_dump row two
array (size=2)
0 =>
array (size=17)
'name' => string 'Yousef' (length=6)
'phase' => string 'اعدادي' (length=12)
'setNumber' => int 1234
'email' => string 'johnef' (length=6)
'sudImage' => string '' (length=0)
'activity' => int 0
'stud_id' => int 2
'id' => int 3
'ins_name' => string 'fonon' (length=5)
'sec_name' => string 'فنون مسرح' (length=17)
'div_name' => string 'شعبة مرجانية 10' (length=26)
'std_id' => int 4
'sub_id' => int 3
'absence' => string '1' (length=1)
'sub_name' => string 'فنون تطبيقية' (length=23)
'subHour' => string '2' (length=1)
'level' => string 'المستوي الأول' (length=25)
1 =>
array (size=17)
'name' => string 'Rida Ali' (length=8)
'phase' => string 'ابتدائي' (length=14)
'setNumber' => int 0
'email' => string 'johnef' (length=6)
'sudImage' => string '' (length=0)
'activity' => int 0
'stud_id' => int 7
'id' => int 3
'ins_name' => string 'fonon' (length=5)
'sec_name' => string 'فنون مسرح' (length=17)
'div_name' => string 'شعبة مرجانية 10' (length=26)
'std_id' => int 4
'sub_id' => int 3
'absence' => string '1' (length=1)
'sub_name' => string 'فنون تطبيقية' (length=23)
'subHour' => string '2' (length=1)
'level' => string 'المستوي الأول' (length=25)
I try to put this condition CROSS JOIN student_absence AS e ON (e.std_id = a.id) but it come's out with one row only
try 2 with INNER JOIN
.....<br/>
FROM student_basic_info AS a
INNER JOIN institutes AS b ON (a.institute = b.id)<br/>
.....
results
it coming out correct but it should come out with 2 rows now it's coming out with one row only.
try 2 with LEFT JOIN
SELECT
a.name, a.phase, a.setNumber, a.email, a.sudImage, a.activity, a.id AS stud_id,
b.id, b.ins_name,
c.id, c.sec_name,
d.id, d.div_name,
e.id, e.std_id, e.sub_id, e.absence,
f.id, f.sub_name, f.subHour, f.level
FROM student_basic_info AS a
INNER JOIN institutes AS b ON (a.institute = b.id)
INNER JOIN ins_sections AS c ON (a.section = c.id)
INNER JOIN ins_division AS d ON (a.division = d.id)
LEFT JOIN student_absence AS e ON (e.std_id = a.id)
LEFT JOIN ins_subjects AS f ON (f.id = e.sub_id)
WHERE a.institute =? AND a.section = ? AND a.division =?
GROUP BY a.id
results
it coming out correct

MySql query result to have nested arrays

I just want my query to return a result looking like this:
array(
[k1] => data1
[k2] => data2
[items] => array(
[item1] => array(
.... data inside
)
[item2] => array(
.... data inside
)
)
)
Here is my query:
SELECT o.*
, ot.tracking_number
, p.id payId
, p.customer_id payCustId
, p.name payName
, p.status payStatus
, p.charge payTotalCharge
, op.*
FROM orders o
JOIN payments o
ON p.id = o.payment_id
LEFT
JOIN orders_tracking ot
ON ot.order_id = o.id
JOIN orderdetails od
ON od.order_id = o.id
JOIN orderproducts op
ON op.orderdetail_id = od.id
WHERE p.customer_id = $customer_id;
What is wrong with my query that it won't return the way I wanted it to?
Thanks.

Selecting the lastest record from a resultset (mysql, php) [duplicate]

This question already has answers here:
Retrieving the last record in each group - MySQL
(33 answers)
Closed 8 years ago.
I have written this code to get some info from my database.
"SELECT
c.from AS user_id,
c.time AS time,
u.user_firstname AS user_firstname,
u.user_lastname AS user_lastname,
u.user_profile_picture AS user_profile_picture
FROM chat c INNER JOIN users u ON u.user_id = c.from WHERE c.to = :id1
UNION SELECT
c.to AS user_id,
c.time AS time,
u2.user_firstname AS user_firstname,
u2.user_lastname AS user_lastname,
u2.user_profile_picture AS user_profile_picture
FROM chat c INNER JOIN users u2 ON u2.user_id = c.to WHERE c.from= :id2
ORDER BY time DESC"
it's work great except one thing. Since this is a inbox script there are many message from same user. but i only need to check if there is a message from this user or not.
array (size=28)
0 =>
array (size=5)
'user_id' => int 6
'time' => string '2014-05-13 19:53:58' (length=19)
'user_firstname' => string 'john' (length=4)
'user_lastname' => string 'doe' (length=3)
'user_profile_picture' => string '6_user_profile.jpg' (length=19)
1 =>
array (size=5)
'user_id' => int 2
'time' => string '2014-05-13 16:59:50' (length=19)
'user_firstname' => string 'james' (length=5)
'user_lastname' => string 'bond' (length=4)
'user_profile_picture' => string '2_user_profile.jpg' (length=19)
2 =>
array (size=5)
'user_id' => int 6
'time' => string '2014-05-13 02:15:44' (length=19)
'user_firstname' => string 'john' (length=4)
'user_lastname' => string 'doe' (length=3)
'user_profile_picture' => string '6_user_profile.jpg' (length=19)
3 =>
array (size=5)
'user_id' => int 6
'time' => string '2014-05-13 02:13:21' (length=19)
'user_firstname' => string 'john' (length=4)
'user_lastname' => string 'doe' (length=3)
'user_profile_picture' => string '6_user_profile.jpg'(length=19)
4 =>
array (size=5)
'user_id' => int 2
'time' => string '2014-05-13 01:58:59' (length=19)
'user_firstname' => string 'james' (length=5)
'user_lastname' => string 'bond' (length=4)
'user_profile_picture' => string '2_user_profile.jpg'(length=19)
as you can see in the var_dump there are 3 john doe and 2 james bond. but I need only the last ones according to the time.
so in this case john doe from 19:53:58, and james bond from 16:59:50. Like this:
array (size=2)
0 =>
array (size=5)
'user_id' => int 6
'time' => string '2014-05-13 19:53:58' (length=19)
'user_firstname' => string 'john' (length=4)
'user_lastname' => string 'doe' (length=3)
'user_profile_picture' => string '6_user_profile.jpg' (length=19)
1 =>
array (size=5)
'user_id' => int 2
'time' => string '2014-05-13 16:59:50' (length=19)
'user_firstname' => string 'james' (length=5)
'user_lastname' => string 'bond' (length=4)
'user_profile_picture' => string '2_user_profile.jpg' (length=19)
and if there are some other users i want to get their last records too.
how can i do this? is this possible with only one query?
try this one I tested.
"SELECT *
FROM (SELECT
c.from AS user_id,
c.time AS time,
u.user_firstname AS user_firstname,
u.user_lastname AS user_lastname,
u.user_profile_picture AS user_profile_picture
FROM chat c INNER JOIN users u ON u.user_id = c.from WHERE c.to = :id1
UNION
SELECT
c.to AS user_id,
c.time AS time,
u2.user_firstname AS user_firstname,
u2.user_lastname AS user_lastname,
u2.user_profile_picture AS user_profile_picture
FROM chat c INNER JOIN users u2 ON u2.user_id = c.to WHERE c.from= :id2
) AS bynames
GROUP BY user_id ORDER BY time ASC"
Put the union in a subquery, order it in the main query, and limit that to the most recent row.
SELECT *
FROM (SELECT
c.from AS user_id,
c.time AS time,
u.user_firstname AS user_firstname,
u.user_lastname AS user_lastname,
u.user_profile_picture AS user_profile_picture
FROM chat c INNER JOIN users u ON u.user_id = c.from WHERE c.to = :id1
UNION
SELECT
c.to AS user_id,
c.time AS time,
u2.user_firstname AS user_firstname,
u2.user_lastname AS user_lastname,
u2.user_profile_picture AS user_profile_picture
FROM chat c INNER JOIN users u2 ON u2.user_id = c.to WHERE c.from= :id2
)
ORDER BY time DESC
LIMIT 1
Untested
SELECT c.user_id
, MAX(c.time)
, u.user_firstname
, u.user_lastname
, u.user_profile_picture
FROM (
SELECT from AS user_id
, time
FROM chat
WHERE to = :id1
UNION
SELECT to AS user_id
, time
FROM chat
WHERE from = :id2
) as c
JOIN users u
ON u.user_id = c.user_id
GROUP BY c.user_id
, u.user_firstname
, u.user_lastname
, u.user_profile_picture

How to optimize a UNION mysql query with multiple select queries?

I've a requirement to show the similar products in product detail page in such a way that the products listed should match the current product properties in a hierarchical way as given below
Size, color, category, company should match with the current product
Size, color, category should match with the current product
Size, color should match with the current product
Size should match with the current product
My sql query is as given below:
(SELECT pd.product_id, pd.name, p.price
FROM mg_product_description pd
JOIN `mg_product` p ON p.product_id = pd.product_id
WHERE
pd.size_id = '33' AND
pd.color_id = '2' AND
pd.category_id = '3' AND
pd.company_id = '1' AND
pd.product_id != '53' AND
p.status = '1'
ORDER BY RAND() LIMIT 10
)
UNION
(SELECT pd.product_id, pd.name, p.price
FROM mg_product_description pd
JOIN `mg_product` p ON p.product_id = pd.product_id
WHERE
pd.size_id = '33' AND
pd.color_id = '2' AND
pd.category_id = '3' AND
pd.product_id != '53' AND
p.status = '1'
ORDER BY RAND() LIMIT 10
)
UNION
(SELECT pd.product_id, pd.name, p.price
FROM mg_product_description pd
JOIN `mg_product` p ON p.product_id = pd.product_id
WHERE
pd.size_id = '33' AND
pd.color_id = '2' AND
pd.product_id != '53' AND
p.status = '1'
ORDER BY RAND() LIMIT 10
)
UNION
(SELECT pd.product_id, pd.name, p.price
FROM mg_product_description pd
JOIN `mg_product` p ON p.product_id = pd.product_id
WHERE
pd.size_id = '33' AND
pd.product_id != '53' AND
p.status = '1'
ORDER BY RAND() LIMIT 10
)
53 - current product id and status denotes available!
Is there any way to optimize the above query?
Note Output required: We need 10 similar products. If there exists 4 products matching condition 1, then they need to listed at first in random order. Similarly we need to list the products matching other conditions below it.
Thanks in Advance!
Keeping using ORDER BY RAND() (which is not efficient) then a very basic improvement would be to add a priority for each clause, move the order clause to the end so it is only needed once.
(SELECT pd.product_id, pd.name, p.price , 1 AS recpriority
FROM mg_product_description pd
JOIN `mg_product` p ON p.product_id = pd.product_id
WHERE
pd.size_id = '33' AND
pd.color_id = '2' AND
pd.category_id = '3' AND
pd.company_id = '1' AND
pd.product_id != '53' AND
p.status = '1'
)
UNION
(SELECT pd.product_id, pd.name, p.price , 2 AS recpriority
FROM mg_product_description pd
JOIN `mg_product` p ON p.product_id = pd.product_id
WHERE
pd.size_id = '33' AND
pd.color_id = '2' AND
pd.category_id = '3' AND
pd.product_id != '53' AND
p.status = '1'
)
UNION
(SELECT pd.product_id, pd.name, p.price , 3 AS recpriority
FROM mg_product_description pd
JOIN `mg_product` p ON p.product_id = pd.product_id
WHERE
pd.size_id = '33' AND
pd.color_id = '2' AND
pd.product_id != '53' AND
p.status = '1'
)
UNION
(SELECT pd.product_id, pd.name, p.price , 4 AS recpriority
FROM mg_product_description pd
JOIN `mg_product` p ON p.product_id = pd.product_id
WHERE
pd.size_id = '33' AND
pd.product_id != '53' AND
p.status = '1'
)
ORDER BY recpriority, RAND() LIMIT 10
That could then be done without the need for unions by doing something like this:-
SELECT pd.product_id,
pd.name,
p.price ,
CASE
WHEN pd.color_id = '2' AND pd.category_id = '3' AND pd.company_id = '1' THEN 1
WHEN pd.color_id = '2' AND pd.category_id = '3' THEN 2
WHEN pd.color_id = '2' THEN 3
ELSE 4
END AS recpriority
FROM mg_product_description pd
JOIN `mg_product` p ON p.product_id = pd.product_id
WHERE pd.size_id = '33'
AND pd.product_id != '53'
AND p.status = '1'
ORDER BY recpriority, RAND()
LIMIT 10

SQL & PHP query with union

I have this query string...
select i.invoiceid, i.date, i.total, i.total - (select ifnull(sum(p.amount), 0) from payment p where p.invoice = i.invoiceid) as remainingbalance
from invoice i inner join client c
on i.client = c.clientid
where i.isdeleted = 0 and i.client = 1
union
select p.paymentid, p.date, p.invoice, p.amount from payment p inner join invoice i
on i.invoiceid = p.invoice
inner join paymenttype
on paymenttype.paymenttypeid = p.paymenttypeid
inner join client c
on c.clientid = i.client
where c.clientid = 1
and i.isdeleted = 0
order by date
when I try this...
<?php
echo $clientArrayInvoice[1]['paymentid'];
?>
I get no results when I do a print_r on on $clientArrayInvoice I get this
Array ( [0] => Array ( [invoiceid] => 1 [date] => 2012-04-12 [total] => 602.29 [remainingbalance] => 300.96 ) [1] => Array ( [invoiceid] => 1 [date] => 2012-04-27 [total] => 1.00 [remainingbalance] => 301.33 ) )
I understand why its doing this, but how do I get the column to say paymentid instead of invoiceid for the results returned. so I can determine what is a paymentid and what is a invoiceid I hope this makes sense.
select i.invoiceid as transactionid, i.date, i.total,
i.total - (select ifnull(sum(p.amount), 0) from payment p where p.invoice = i.invoiceid) as remainingbalance,
'invoice' as transaction_type
from invoice i inner join client c
on i.client = c.clientid
where i.isdeleted = 0 and i.client = 1
union
select p.paymentid as transactionid, p.date, p.invoice, p.amount, 'payment' as transaction_type
from payment p inner join invoice i
on i.invoiceid = p.invoice
inner join paymenttype
on paymenttype.paymenttypeid = p.paymenttypeid
inner join client c
on c.clientid = i.client
where c.clientid = 1
and i.isdeleted = 0
order by date

Categories