I want to display only the duplicated results in a PHP page, not their count and there is a condition where certain field can't be empty. What is the mysqli query for displaying each duplicated result and not grouping them so it displays all the duplicates in 1 row rather than displaying each duplicate in a single row?
This is for a reporting panel on full PHP website. I have tried some queries like SELECT col1,col2 FROM table Where col3!='' GROUP BY col2 HAVING COUNT(col1) > 1; But this will display the duplicated results in 1 row, if I have 3 duplicates with same col2 the query returns only one due to the GROUP BY col2 clause, I tried GROUP BY col2,col1 but now HAVING COUNT(col1) > 1 can never be true since it is impossible to have same value of col1.
I expect the output of the query to display each duplicated result in a row and not group them in 1 row only. In other words to display the same 3 duplicated results having same col2 but different other columns values and not display only their 1 result.
All results:
Query I tried:
Try this
SELECT * FROM usersl Where hash!='' AND user_fullName IN (SELECT user_fullName FROM usersl HAVING COUNT(user_fullName) > 1);
This will give you result as you want
I think you should add one more column in group by clause and that column should be Primary ID column then you will got your desired result.
SELECT col1,col2 FROM table Where col3!='' GROUP BY col2,pk_id;
Try this. I think this helps you.
SELECT u1.* FROM usersl AS u1
INNER JOIN (SELECT user_fullName FROM usersl WHERE hash != '' GROUP BY (user_fullName) HAVING COUNT(user_fullName) > 1) AS u2 ON u1.user_fullName = u2.user_fullName
WHERE u1.hash!=''
First find all ID's of these fields:
$ids = $db->queryColumn(
"SELECT GROUP_CONCAT(id SEPARATOR ',') FROM table WHERE col3 != '' GROUP BY col2 HAVING COUNT(col1) > 1"
);
Then make array of ID's:
$idsToFind = [];
foreach ($ids as $idString) {
array_push($idsToFind, array_walk('trim', explode(',', $idString)));
}
Now do another query where you find elements by ID:
$db->queryAll(
'SELECT col1, col2 FROM table WHERE id IN :idArray',
$this->escapeArray($idsToFind)
);
Related
I want make a leaderboard from my database, I already make a code but the sql says error
"SET #a1 = (SELECT SUM(d.poin_diskusi)
FROM diskusi d
GROUP BY d.id_akun)
MySQL said: Documentation
#1242 - Subquery returns more than 1 row"
heres my sql query :
SET #a1 = (SELECT SUM(d.poin_diskusi)
FROM diskusi d
GROUP BY d.id_akun);
SET #a2 = (SELECT SUM(j.nilai_jawaban)
FROM jawaban j
GROUP BY j.id_akun);
SET #hasil = #a1 + #a2;
SELECT #hasil
Thank you !
this is an example data in table jawaban:
this is an example data in table diskusi:
and my desired answer:
where tera123 have 135 TOTAL(d.poin_diskusi+j.nilai_jawaban) and david123 have 90 TOTAL(d.poin_diskusi+j.nilai_jawaban)
the point is to show TOTAL for every user
It looks to me (without looking at your images; if you want to show information, please provide it in text) like you want something like:
select id_akun, sum(point_value) as total_points from (
select id_akun, sum(poin_diskusi) as point_value
from diskusi group by id_akun
union all
select id_akun, sum(nilai_jawaban) as point_value
from jawaban group by id_akun
) as point_values
group by id_akun
The problem are the GROUP BY keywords. Inside the images you provided with example data, one can see that there are multiple entries for the columns jawaban.id_akun and diskusi.id_akun. Because of that, both SELECT will return multiple rows as result, for each different value of the id columns one summed up row. Try it without the GROUP BY clause.
Below is the source table and the output I am trying to achieve.
I would like each another_table.id to only appear once in the output, with subsequent values displaying in new columns
This will give you the output, given the table in your question. With CTEs instead of subqueries for added clarity...
with intials as(
SELECT id,
another_table.id as another_table_id,
value as field_1
FROM
main_table
WHEN
variable.id = 1),
finals as(
SELECT id,
another_table.id as another_table_id,
value as field_1
FROM
main_table
WHEN
variable.id = 2)
SELECT
initials.id,
initials.another_table_id
initials.value field_1,
finals.value field_2
FROM
initials
LEFT OUTER JOIN
finals on initials.another_table_id = finals.another_table_id;
How can I select one row from that table http://i.stack.imgur.com/27cu9.jpg where values of 'user_1' and 'user_2' may look like
user_1 user_2
1 2
2 1
In other words I want to select a field that contains 2 users with submitted=1 no matter in which field the value is.
Here is a simple query that does this:
select *
from t
where submitted = 1 and 2 in (user_1, user_2)
If I understood your question, I think you need to JOIN the table on itself if you are trying to return rows that have corresponding users (1,2) and (2,1):
select t1.*
from yourtable t1
join yourtable t2 on
t1.user_1 = t2.user_2 and t1.user_2 = t2.user_1
SQL Fiddle Demo
If however you are just trying to see if user 2 exists in either of the fields, then look at Gordon's post.
Use this:-
select * from tblname as t1, tblname as t2 where
t1.user_1 = t2.user_2 and t1.user_2 = t2.user_1 and t1.user_1<>t1.user_2
EDIT:-
Updated the query so that the rows with the same values do not appear in the result.
I have a reviews table that contains three ways to rate an item. The items themselves then have three columns to hold the average for each value respectively.
I could do this using three nested queries in an update query, but I feel like this is inefficient... Is there a way to update them all at once?
So far I've used this as my select query:
SELECT AVG(rating_1),AVG(rating_2),AVG(rating_3) FROM items_reviews WHERE item_id = 1
I just don't know how to use the result of that query to update an item row.
You could use an join in the UPDATE:
UPDATE items a
INNER JOIN
(
SELECT
item_id,
AVG(rating_1) AS avg1,
AVG(rating_2) AS avg2,
AVG(rating_3) AS avg3
FROM items_reviews
WHERE item_id = 1
GROUP BY item_id
) b ON a.item_id = b.item_id
SET
a.avgrating1 = b.avg1,
a.avgrating2 = b.avg2,
a.avgrating3 = b.avg3
What i want, to display rows from a table which is selected by a field from other table single value, lets say to display images from a table by last category id.
I have this type of query, but this return me all matching keys rows, if i inset LIMIT 1 then it return one row...
SELECT i.prof_image FROM profile_images i
JOIN images_cat cat ON (cat.cat_id = i.cat_id)
GROUP BY i.prof_image;
//OR LIMIT 1;
Any idea to fix this problem. (i.e. displaying the latest category images)?
This will work for your specific example.. If you need to be more selective, then please post some more details..
SELECT i.prof_image
FROM profile_images i
WHERE cat_id = (select max(cat_id) from images_cat)
SELECT * FROM table_1
LEFT JOIN table_2 ON table_1.id = table_2.id
This query will grab all things in table_2 that have the same id value.
Note that it is a LEFT JOIN - which means that if there are no matching values in table_2, it will still return the values from table_1.
What is your intent with using last()?
Hope this helps.