Selecting different rows with same id but different values - php

I have a table above, I want to get the record_id(3 in this case) having meta_name=discount_percent and meta_value=56 combined with another condition meta_name=discount_multiplier and meta_value=30. I tried using HAVING clause with GROUP BY but it didn't work.
Would appreciate if someone can help.

I believe you want to get the records with your two conditions group them by the ID and check if the count of rows in the group is 2, so that both conditions applied to that ID (assuming that the pair of record_id and meta_name is unique).
SELECT record_id
FROM elbat
WHERE meta_name = 'discount_percent'
AND meta_value = '56'
OR meta_name = 'discount_multiplier'
AND meta_value = '30'
GROUP BY record_id
HAVING count(*) = 2;

Related

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

Multiple Where clause using same columns SQL

I have table like this :
user_id field_id value
1 1 toto
2 1 tata
2 2 tata Job
user_id is the id of my users, field_id is the id of the information about the user (name, job, etc.) and value is the value.
I use a searchBar in HTML/PHP with 2 fields : 'name' and 'job'.
When someone search user using this two fields I need to retrieve the user id that matches these two conditions.
My question is :
How to get in SQL all user_id that match with two condition in one like this:
(field_id=1 and value=tata) AND (field_id=2 and value=tata's Job)
Thank you and have a good day !
SQL executes the WHERE logic for each row, not for the entire set. And fields cannot have more than one value. So, you need to use an OR statement instead of an AND to capture both conditions:
Select User_Id, Field_Id, Value
From YourTable
Where (field_id = 1 And value = 'tata')
Or (field_id = 2 And value = 'tata Job')
One method uses aggregation:
select user_id
from t
where (field_id = 1 and value = 'tata') or (field_id = 2 and value = 'tata''s Job')
group by user_id
having count(distinct field_id) = 2;
You can use IN statement, for example: Your table name is users
SELECT user_id
FROM users
WHERE field_id IN (1,2) AND `value` IN ('tata','tata\'s Job')

query to Ignore the repeated column in leaderboard

i have a problem my database looks like this DATABASE STRUCTURE
i want to sort the lboard and ignore the repeated values
like this REQUIRED OUTPUT
can anyone help me?
You could use GROUP BY to get single result for one user and MAX function to get its maximum score
the query will be like this
SELECT id, exam_id, user_id, MAX(lboard) FROM `leaderb` GROUP BY user_id ORDER BY lboard DESC
SELECT id, exam_id, user_id, MAX( lboard )
FROM wp_watu_takings WHERE exam_id = 3
GROUP BY user_id
ORDER BY MAX( lboard ) DESC

Count entries from second Table that match id from first Table

This is basic but I can't figure it out. I have two tables (SeriesTable and OtherTable). SeriesTable has all the information for a given series including its id (column named "seriesid"). And OtherTable has a column called "seriescolumn" which also has the ids of a given series.
I need to make a query that counts every entry in OtherTable's "seriescolumn" that matches the seriesid column in SeriesTable. So for example, if the seriesid in SeriesTable is 5, I need to count how many entries in OtherTable have the value of 5 in the seriescolumn.
Below is my current code that simply grabs the info from the first table, but I have no idea how to correctly count the matching entries from OtherTable.
<?
$rs= mysql_query("SELECT seriesid FROM SeriesTable ORDER BY seriesid DESC");
while ($row= mysql_fetch_array($rs)) { ?>
content
<? } ?>
Sounds like you are going to need a join and group by statement.
SELECT s.seriesid, Count(*) As NumberOfSeries
FROM SeriesTable s Join
OtherTable o On s.seriesid = o.seriescolumn
Group By s.seriesid
ORDER BY seriesid DESC
This should return each seriesid and a count of how many times it was repeated.
Probably the easiest way to do this is in one big SQL query, using the count statement.
You can use a GROUP BY clause to group the result by the seriesid as you want, giving something along the lines of:
SELECT seriesid, COUNT(*) FROM SeriesTable, OtherTable
WHERE seriescolumn=seriesid GROUP BY seriesid
SELECT seriesid, COUNT(seriescolumn)
FROM SeriesTable, OtherTable
WHERE OtherTable.seriescolumn = SeriesTable.seriesid
GROUP BY seriesid;

trying to output the correct value from SQL query from comparing a different table

I'm very new with SQL and need assistance on how I can accomplish this task using the correct query.
I have 2 tables that I need to use. Table "TB1" has:
id Name
1 bob
2 blow
3 joe
table "TB2" has:
compid property
1 bob
2 blow
I am trying to get which compid is missing in "TB2" and insert it from "TB1"
the query I am doing is:
SELECT id, name from TB1, TB2 where id <> compid
what I get is 2 ouputs of Id 1, and 2, and 3 outputs from id 3. by using php:
for($i=0;$i <= mysql_num_rows($comp)-1; $i++)
{
echo mysql_result($comp, $i, 0)."<br>";
}
and I expected the ouput 3 but instead got this:
1
1
2
2
3
3
3
I understand its comparing all the rows within the table but is there a way to achieve what I am looking for?
Thanks for your time.
You are performing an implicit Cartesian JOIN which results in every row against every other row. You need to specify what attribute JOINs the two tables.
Using implicit syntax (not recommended):
SELECT id, name
FROM TB1, TB2
WHERE id <> compid
AND TB1.Name = TB2.property <-- Column join
Using explicit syntax:
SELECT id, name
FROM TB1
JOIN TB2
ON TB2.property = TB1.Name <-- Column join
WHERE id <> compid
To accomplish your goal you would need something along the lines of:
SELECT TB1.id, TB1.name
FROM TB1
LEFT JOIN TB2
ON TB2.property = TB1.Name
WHERE TB2.compid IS NULL
See it in action
It's best practice to always alias the columns you select to prevent ambiguity.
To select it you can do:
SELECT *
FROM TB1
WHERE id NOT IN (
SELECT compid
FROM TB2
);

Categories