PHP Group by and list all the other column - php

I have a table:
The select db is:
$select_table2 = '
SELECT e.product_clicks
, e.product_id
, e.website_url
, u.name
, u.product_id
FROM `oc_aa_affiliatecollclicktracking` AS e
LEFT
JOIN `'.DB_PREFIX.'product_description` AS u
ON e.product_id = u.product_id
GROUP
BY e.website_url';
This will group it but won't list all product_id
I get:
http://127.0.01
36
I'd like to group it but see all the product_id
http://127.0.01
36
40
33

$select_table2 = 'SELECT `product_id` FROM `oc_aa_affiliatecollclicktracking` WHERE website_url LIKE '%http%';
if you dont want to repeat product_id
$select_table2 = 'SELECT DISTINCT(`product_id`) FROM `oc_aa_affiliatecollclicktracking` WHERE website_url LIKE '%http%';

Related

How to give CASE WHEN THEN in mysql where clause based on select columns?

I am using select query and selection columns in which i have to get total count of a column as well as condition based count
for total count column name in select query is ims_total_count (condition is_correct IN (1,0))
for column name ims_code_count condition is_correct = 0
i paste my code below
SELECT q.ims_code,
COUNT(q.ims_code) AS ims_total_count,
M.NAME,
M.score_board_order,
M.id AS exam_id,
M.view_question_hr ,
M.section_id,
GROUP_CONCAT(DISTINCT M.section_id) AS coded_section,
GROUP_CONCAT(DISTINCT REPLACE(SUBSTRING_INDEX(M.NAME,'Sec', 1) ,'New ','') ) AS coded_test_name,
COUNT(q.ims_code) AS ims_code_count,
q.question_password,
qcat.NAME AS topic_name,
qsubcat.NAME AS cat_name,
qsubcat.description AS sub_cat_name,
GROUP_CONCAT( DISTINCT UPPER( REPLACE(LOWER(q.question_code),'new','') ) ) AS coded_que,
GROUP_CONCAT( DISTINCT q.id ) AS coded_que_id,
GROUP_CONCAT(q.exam_id) AS coded_exam_id
FROM y2a_watupro_question AS q INNER JOIN y2a_watupro_master AS M
ON q.exam_id = M.id LEFT JOIN y2a_watupro_qcats AS qcat
ON q.cat_id = qcat.id LEFT JOIN y2a_watupro_qsub_cats AS qsubcat
ON q.sub_cat_id = qsubcat.id
WHERE q.id IN (
SELECT question_id
FROM y2a_watupro_student_answers
WHERE CASE is_correct WHEN ims_total_count THEN '1,0'
ELSE '0' END AND taking_id IN (
SELECT t.id
FROM y2a_watupro_taken_exams AS t INNER JOIN y2a_test_score_board_section AS s
ON s.id = t.section_id
WHERE s.sc_id =0
AND t.user_id=5270
AND t.trial_status = 1
AND t.in_progress = 0
AND t.isactive = 1 )
)
GROUP BY M.score_board_order, q.ims_code
ORDER BY M.score_board_order ASC;
SELECT *,CASE
WHEN OCCUPATION = 'I am Salaried' THEN 'Salaried'
WHEN OCCUPATION = 'I am not Employed' THEN 'Student'
WHEN OCCUPATION = 'I am Retired' THEN 'Retired'
WHEN OCCUPATION = 'I am Self Employed' THEN 'Self Employed'
WHEN OCCUPATION = 'I Run Business' THEN 'Own Business'
WHEN OCCUPATION = 'I am a Professional (Dr/CA/LLB/Engg etc)' THEN 'Professional'
ELSE 'OCCUPATION'
END AS OCCUPATION FROM TABLE_NAME;
This is works fine for me.

How to group by a column with forloop and union all

Hi this is my query from this query i am getting 3 records i want only one record and i am trying to group by studentid
foreach ($gdFinaldata['gdskill'] as $skillId => $skillScore){
$filterQuery .= $union_all. "SELECT stu.student_pid
FROM tbl_students stu LEFT JOIN r_job_invitations jbi ON stu.student_email = jbi.email
LEFT JOIN r_job_scores jsc ON jsc.student_id = stu.student_pid
WHERE job_id = ".$jobID." AND skill_id = ".$skillId." AND gd_score >= ".$skillScore." ";
$union_all=" UNION ALL ";
} //for ends here
the recodrs will come like this:
SELECT stu.student_pid FROM tbl_students stu LEFT JOIN r_job_invitations jbi ON stu.student_email = jbi.email LEFT JOIN r_job_scores jsc ON jsc.student_id = stu.student_pid WHERE job_id = 88 AND skill_id = 3 AND gd_score >= 1
UNION ALL
SELECT stu.student_pid FROM tbl_students stu LEFT JOIN r_job_invitations jbi ON stu.student_email = jbi.email LEFT JOIN r_job_scores jsc ON jsc.student_id = stu.student_pid WHERE job_id = 88 AND skill_id = 63 AND gd_score >= 2
UNION ALL
SELECT stu.student_pid FROM tbl_students stu LEFT JOIN r_job_invitations jbi ON stu.student_email = jbi.email LEFT JOIN r_job_scores jsc ON jsc.student_id = stu.student_pid WHERE job_id = 88 AND skill_id = 128 AND gd_score >= 3
After this i want to add group by how can i do that i tried like this:
$groupby = "GROUP BY student_id";
$filterStudents = $conn->query($filterQuery.$groupby);
My issue is still returning 3 records i want to group by student
For Reference:
There are several ways to do this.
Use union instead of union all, this will retain only unique entries
select ...
union
select ...
union
select ...
If you want to keep union all (why do you?), use parenthesis around your selects and append group by after it
select student_pid
( select student_pid, student_id from ...
union all
select student_pid, student_id from ...
union all
select student_pid, student_id from ... ) as t
group by student_id
Your PHP would then look like
foreach (...) {
$filterQuery .= $union_all. "SELECT stu.student_pid, jsc.student_id ..."
// ...
}
$outerQuery = "select student_pid ("
$groupby = ") as t GROUP BY student_id";
$filterStudents = $conn->query($outerQuery . $filterQuery . $groupby);
Instead of group by, you can also use distinct, e.g.
select distinct student_pid
( select student_pid from ...
union all
select student_pid from ...
union all
select student_pid from ... ) as t
Collect the result in an array and then use array_unique
Although this is the least efficient, since it fetches all entries instead of the relevant parts.

How to fetch data from database in Magento with LIKE keyword?

I want a select query which fetch the data from database with "like" keyword
eg.
select product name, image , description
from table where product name LIKE "'.$name'";
I want it in Magento that how to fetch this fields from database in Magento. My existing query is:
$sql = "
SELECT value FROM catalog_product_entity_varchar
WHERE entity_type_id = (
SELECT entity_type_id FROM eav_entity_type WHERE entity_type_code = 'catalog_product') AND attribute_id = (SELECT attribute_id FROM eav_attribute WHERE attribute_code = 'name' AND entity_type_id = (SELECT entity_type_id FROM eav_entity_type WHERE entity_type_code = 'catalog_product')
)
";
Now the join query is :-
<?php
$sql = "
SELECT p.entity_id , pv.value as name , pt.value as description , GROUP_CONCAT (DISTINCT ( cp.category_id ) SEPARATOR ', ') as categories , GROUP_CONCAT (DISTINCT (pm.value) SEPARATOR ', ') as imagesPath
FROM catalog_product_entity as p
INNER JOIN catalog_product_entity_varchar as pv on pv.entity_id = p.entity_id and pv.attribute_id = 71
INNER JOIN catalog_product_entity_text as pt on pt.entity_id = p.entity_id and pt.attribute_id = 72
INNER JOIN catalog_category_product as cp on cp.product_id = p.entity_id
LEFT JOIN catalog_product_entity_media_gallery as pm on pm.entity_id = p.entity_id and pm.attribute_id = 88
GROUP BY cp.product_id , pm.entity_id " ;
foreach($readConnection->fetchAll($sql) as $orders)
{
?>
<?php echo $orders['name']; ?>
<?php echo $orders['description']; ?>
<?php
}
?>
This one is correct code to fetch the name and description of item in LIKE clause
Thank you so much #Halfer :)
$sql = SELECT p.`entity_id`, pv.`value` as name, pt.`value` as description
FROM `catalog_product_entity` as p
INNER JOIN `catalog_product_entity_varchar` as pv on pv.`entity_id` = p.`entity_id` and pv.`attribute_id` = 71
INNER JOIN `catalog_product_entity_text` as pt on pt.`entity_id` = p.`entity_id` and pt.`attribute_id` = 72
Where pv.value LIKE '%hp%';
$sql_jacket = "SELECT entity_id FROM customer_entity where email
like '".$_REQUEST['useremail']."'";
$result_jacket = $conn->query($sql_jacket);
if ($result_jacket->num_rows > 0)
{
while($row_jacket = $result_jacket->fetch_assoc())
{
$result1['user_status']=1;
}
}

Select with inner join

i need select some data from two tables ,
please help me use inner join for this selection .
players in selction2 must not be in selection1...
first select :
$rs = "SELECT *
FROM `player`
WHERE `status`=1 AND `credit`>=1 AND `username` NOT LIKE '$user'
ORDER BY ls ASC,credit DESC
LIMIT 0 ,10;
Second: this players must remove from result of selection1
$rs2 = "SELECT *
FROM `ip_log`
WHERE `playerid`='$ui' AND `win`='1' AND `date`='$date' ";`
You can use LEFT JOIN for this:
This shows the log messages for everyone not in selection 1.
SELECT l.*
FROM ip_log AS l
LEFT JOIN
(SELECT username
FROM player
WHERE status = 1 AND credit >= 1 AND username NOT LIKE '$user'
ORDER BY ls ASC, credit DESC
LIMIT 10) AS p
ON l.player = p.username
WHERE win = 1 and date = '$date'
AND p.username IS NULL
This shows the top 10 player data, except the ones with log messages in selection 2
SELECT p.*
FROM player AS p
LEFT JOIN ip_log AS l ON l.player = p.username AND l.win = 1 AND l.date = '$date'
WHERE p.status = 1 AND p.credit >= 1 AND p.username NOT LIKE '$user'
AND l.player IS NULL
ORDER BY p.ls ASC, p.credit DESC
LIMIT 10
In both cases, testing a column in the second table with IS NULL makes it return only the rows in the first table that don't have a match in the second table. See
Return row only if value doesn't exist
You can do it with LEFT JOIN
SELECT player.*,ip_log.* FROM `player` LEFT JOIN `ip_log` ON player.id!=ip_log.playerid GROUP BY player.id

join two queries to form an result set

hello all i have to join two queries to form an result set :-
1st:
SELECT
orderId,
GROUP_CONCAT(DISTINCT(categoryId) ORDER BY orderId SEPARATOR ', ') as catId
FROM
ecart_product
INNER JOIN ecart_orderdetail
WHERE
ecart_orderdetail.productId = ecart_product.id
group by orderId
this gives me
orderId catId
167 59, 2
168 2
169 2
170 2
171 2
172 48, 2
173 2
174 2
2nd:
select * from ecart_orders
in both orderId is common how could this could be join ?
Assuming that orderId is PK in ecart_orders table and that it has productId column.
You can try this:
SELECT
orderId,
GROUP_CONCAT(DISTINCT(categoryId) ORDER BY orderId SEPARATOR ', ') as catId ,
ecart_orders.*
FROM
ecart_product
INNER JOIN ecart_orderdetail
ON ecart_orderdetail.productId = ecart_product.id
INNER JOIN ecart_orders
ON ecart_orders.productId = ecart_product.id
group by orderId
In mean time i also solved it
select * from ecart_orders Inner join(
SELECT
orderId,
GROUP_CONCAT(DISTINCT(categoryId) ORDER BY orderId SEPARATOR ', ') as catId
FROM `ecart_product` INNER JOIN ecart_orderdetail
WHERE ecart_orderdetail.productId = ecart_product.id group by orderId) as c
on ecart_orders.id=c.orderId

Categories