Multiple SELECT, Avoid duplicate results for one table in MySQL query - php

How can I select from two tables where table 1 returns 1 row and table 2 returns multiple rows resulting in NO duplicates from table 1?
Below, page.title has one row. page_images.image_loc can have anything from 1 to 10. If page_images.image_loc has 2 or more, page.title will be duplicated for the count of page_images.image_loc rows. How can I limit page.title to 1, but not page_images.image_loc?
$query = mysql_query("
SELECT page.title,
page_images.image_loc
FROM page, page_images
WHERE page.url_category = '$category' AND
page.url_title = page_images.page_title
");

You could try SELECT page.title, GROUP_CONCAT(page_images.image_loc) AS image_locs ... GROUP BY page.title to collect all the image_loc values in one row, then separate the list with explode().

Related

Select rows from 2 tables in 1 query

Need to select name,domain from table 1, than I need a sum of values from a column from table 2 with condition table1.id = table2.
Table 2 does not have the same number of columns.
I have tried joining 2 queries with UNION and UNION ALL, but i keep getting the same problem of different number of columns.
$rows = $test->query('select domain,name from customers
UNION
select SUM(customer_id) from main.rentals,main.customers where customer_id = customers.id');
Expected would be "User's Name" - "domain" - "number of rentals(integer)"
Warning: SQLite3::query(): Unable to prepare statement: 1, SELECTs to the left and right of UNION do not have the same number of result columns
If you use SUM, it will sum all ids, for example if you have a customer ID of 10 and it shows 5 times the result will be 50. In the other hand if you use count it will count the rows that that id was shown. That's why we are doing a group by. You may tweak it to fill your specific needs, but this is one way to achieve what you want.
$query = "
SELECT
domain,
name,
count(customer_id) as Total
FROM
customers
left join main.rentals on customers.id = customer_id
GROUP BY
customer_id
";
$test->query($query );

How to display duplicated results only in MYSQLI?

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)
);

Sum values in multidimensional array (selecting multiple columns with multiple rows)

Let's say I have 5 different columns, a, b, c, d, e, and I'm selecting multiple rows:
$result = mysqli_query($conn,"SELECT a,b,c,d,e FROM posts WHERE submitter='$user'");
while ($row = mysqli_fetch_assoc($result)){
$ratings[] = $row;
}
Example:
The user has 3 posts, so it'll select 3 rows in the query.
I want to sum all of the rows' values for a (and the rest of course).
e.g.
row 1 a value = 4
row 2 a value = 10
row 3 a value = 1
So I need to sum all of those to get 15.
I know to use array_sum($ratings) to find the sum of the array but only if you select one column (a) which can have multiple rows, but this is multi-dimensional right due to multiple column values being selected?
You can just use sum in your query:
select sum(a)
, sum(b)
, sum(c)
, sum(d)
, sum(e)
from posts
where submitter = '$user'
You can use count aggregate function and group by in MySQL.
SELECT
submitter,
count(a) as rating
FROM posts
WHERE submitter='$user'
GROUP BY submitter
A a result you will get something like that:
some submitter, 3
another submitter, 10
one more submitter, 1
Is this helpful?

Select count for each distinct row (mysql and php)

I am using mysql and php.
I have a table with one column. I can show unique rows by:
select distinct id from id_table
This might show
1
2
3
5
What I want to do is show the number of 1's, 2's, etc there are.
I could do
select count(id) from id_table where id = 1
But then I have to run a query for every... single... row... And with hundreds of thousands of rows, not good.
Is there a way to return an array of the counts of each distinct item
Data
1, 1, 1, 2, 3, 3, 5
Results
3, 1, 2, 1
Thanks
select id, count(id)
from table
group by id
If only want count of ids, then
select count(id)
from table
group by id
Here is a simple tutorial of group by clause
http://www.w3schools.com/sql/sql_groupby.asp

count the number of rows in mysql?

How to count the number of rows in mysql database tables with php?
if have in database 5 rows, they number show like this(bold):
all columns is: 5
1 row1 2 row2 3 row3 4 row4 5
row5
Just use this SQL query:
SELECT COUNT(*) FROM table_name
If you want to know how many rows were returned by another query, you can use the PHP mysql_num_rows function. (You could also just increment a counter for each row you process, but this function is handy if you need to know the number of records prior to enumerating the results.)
How do we count them back together? LIKE: 1 2 3 4 5. i not want use of id in column database
select list_of_fields,#rn:=#rn+1 as row_num
from table,(select #rn:=0) as r order by id
You can use this
$result = $this->db->get(<table_name>);
$num_rows = $result->num_rows();
$num_rows would be the total rows in table_name
Then you can just do this
echo 'The number of rows in table_name is '.$num_rows;
This query should do the trick for you:
SELECT COUNT(*) as count FROM `table`
The result set would be
[count]
-------
5
Assuming you have 5 rows.
In order to manually count each row and display its index (without using ID), I would do something like
$counter = 1;
$stmt = $db->prepare('SELECT `field` FROM `table`');
$stmt->execute();
while($row = $stmt->fetch()) {
echo "<b>{$counter}:</b> {$row['field']}";
$counter++;
}

Categories