You're help would be much appreciated...
If I have the following table and sample data... myGroupTable
group_Id : user_Id
1 : 3
1 : 7
1 : 100
2 : 3
2 : 7
2 : 100
2 : 104
4 : 42
4 : 98
4 : 13
I would like a sql statement that would...
Return a group_Id that has exactly the specified user_Id's in them.
eg... is there a group_Id that has User_Id's 3, 7 and 100
answer: group_id 1.
Please note that I dont want it to return a group_Id 2, as that also has a user_Id of 104 in it...
Kind regards J
SELECT
group_Id,
SUM(
IF user_Id = 3 THEN 1
ELSEIF user_Id = 7 THEN 2
ELSEIF user_Id = 100 THEN 4
ELSE 8
) AS bits
FROM myGroupTable
GROUP BY group_Id
HAVING bits=7
This assumes that you cannot have duplicate user_Ids for the same group_Id, eg this could never happens:
group user
1 3
1 3
Edit: You can build your query in the following way:
<?php
$ids = array(3, 7, 100);
$power = 2;
$query = "
SELECT
group_Id,
SUM(
IF user_Id = " .$ids[0]. " THEN 1 ";
foreach ($id in $ids) {
$query .= " ELSEIF user_Id = $id THEN " . $power;
$power = $power * 2;
}
$query .= " ELSE $power
) AS bits
FROM myGroupTable
GROUP BY group_Id
HAVING bits = " . ($power - 1);
Here's another alternative:
SELECT group_id, GROUP_CONCAT(user_id ORDER BY user_id) AS user_id_list
FROM group_user
GROUP BY group_id
HAVING user_id_list = '3,7,100'
another solution:
SELECT group FROM tbl WHERE group_id NOT IN (SELECT DISTINCT group_id FROM tbl WHERE user_id NOT IN(3,7,100)) AND user_id IN (3,7,100);
select group_id,
sum(
case user_id in(3,7,100)
when 1 then 1
else -99
end
) as must_be_3
from group_user
group by group_id
having must_be_3=3;
Related
I want to switch the values if a condition is added to the sql query but i don't know exactly how work the if else statement in sql.
This is my query:
DATABASE:
attr_id
st_id
prod_id
value
Data:
$attr_id1 = "75";
$attr_id2 = "76";
$st_id = array('2','2','2','2');
$prod_id = array('2','3','4');
$value = array('150','200','300');
SQL:
"INSERT INTO table
(attr_id, st_id, prod_id, value)
VALUES (IF(value from $attr_id2 is < less that $value switch to $attr_id2 else to $attr_id1), ".$st_id.", ".$prod_id.", ".$value.")
ON DUPLICATE KEY UPDATE
value = (".$value.")";
"WHERE attr_id = ".$attr_id1." //OR ATTR_ID2
AND st_id = ".$st_id."
AND prod_id = ".$prod_id."
"
DATATABLE:
attr_id st_id prod_id value
75 2 2 150
76 2 2 200
75 2 3 300
76 2 3 290
76 2 4 200
Any help is appreciated.
You can use CASE
INSERT INTO TABLE (attr_id, st_id, prod_id, VALUE)
VALUES
(
CASE
WHEN ".$attr_id2." < ".$attr_id1."
THEN ".$attr_id2."
ELSE ".$attr_id1."
END,
".$st_id.",
".$prod_id.",
".$value."
) .....
i have used the following query to select rows from tables.
Table 1:
id description status add_date topicid
1 xyz 0 22-3-13 5
2 pqr 0 21-3-13 5
3 abc 0 20-3-13 5
4 sdd 0 22-3-13 5
Table2:
id otherid
1 2
2 3
This query gives me all the record from table1 but i want to select those record which is not in table2.
like table1 'id' not present in table2 'otherid'.
In my case want to select record from table1 for id 1 and 4.because which is not present in table2 as 'otherid'.
$topicid = 5;
$q =$this->db->select(array(
't1.id as id',
't1.description',
't1.topicid',
't1.add_date'))
->from('table1 AS t1')
->where('t1.topicid',$topicid)
->where('t1.status',0)
->order_by('t1.add_date DESC)->get();
try this query will work
$topicid = 5;
$q =$this->db->select(array(
't1.id as id',
't1.description',
't1.topicid',
't1.add_date'))
->from('table1 AS t1')
->where('t1.topicid',$topicid)
->where('t1.status',0)
->where('t1.id NOT IN (select otherid from table2)',NULL,FALSE)
->order_by('t1.add_date DESC)->get();
$select = array(
't1.id as id',
't1.description',
't1.topicid',
't1.add_date'
);
$this->db
->select($select)
->join('Table2','Table2.otherid = Table1.id','left')
->where('Table2.otherid IS','NULL')
->where('Table1.topicid',$topicid)
->where('Table1.status',0)
->get('Table1');
i have a table called ptb_profile_views that looks like this:
id | profile_id | viewed_profile_id
1 2 6
2 2 6
3 3 6
4 -1 6
5 -1 6
i have been trying to count the positive values in 'profile_id' only once as distinct values and count -1 values as many times as they appear with the following query:
function check_profile_views3() {
global $connection;
global $_SESSION;
$query = "
SELECT id,profile_id,viewed_profile_id COUNT
FROM ptb_profile_views
WHERE profile_id > 0
GROUP BY profile_id
UNION
/*shows all -1 profile id's*/
SELECT id,profile_id,viewed_profile_id
FROM ptb_profile_views
WHERE profile_id <= 0";
$check_profile_views_set3 = mysql_query($query, $connection);
confirm_query($check_profile_views_set3);
return $check_profile_views_set3;
}
so the end result is
2, 3, -1, -1
but instead of echoing out the actual values them selves i want the query to do a count of the values.
so...
2, 3, -1, -1 = a total of 4
i am also trying to call the query like so which i don't know if will work:
$check_profile_views_set3 = check_profile_views3();
while ($views3 = mysql_fetch_array($check_profile_views_set3)) {
echo "".$views3['profile_id'].""; ?>
can someone please show me how i can do this? thanks
SELECT a + b AS TotalCount
FROM
(
SELECT COUNT(DISTINCT profile_ID) a
FROM ptb_profile_views
WHERE profile_ID > 0
) x,
(
SELECT COUNT(profile_ID) b
FROM ptb_profile_views
WHERE profile_ID < 0
) y
SQLFiddle Demo
I have this query that retreives a list of id's + NAME:
$sql = "SELECT id FROM #__table1 ";
$sql .= " WHERE (";
foreach($explode_tags as $k=>$explode_tag) {
$sql .= "name = ".$db->Quote(trim($explode_tag));
if(($k+1) != count($explode_tags))
$sql .= " OR ";
}
$sql .= ")";
$db->setQuery($sql);
$results = $db->loadResultArray();
The result is an array like this:
keywordID | NAME
1 cat
2 dog
3 horse
Now I have this table2:
id | ItemID | keywordID
1 4 1
2 4 2
3 4 3
4 6 1
5 6 2
6 7 1
I want to find from table2 all ItemID's that have all keywordID's found in table1.
In the example above I want to return only itemID 4 that has all keywords (all 3 of them).
I am running this query but I am not getting results:
...
$query .= " AND i.id IN (SELECT itemID FROM #__table2 WHERE (";
foreach($results as $k=>$result) {
$query .= "keywordID = ".(int)$result;
if(($k+1) != count($results))
$query .= " AND ";
}
$query .= "))";
UPDATE
Sorry, I miss read your question. I've done simple test using this data:
id itemId keywordId
1 4 1
5 4 2
6 4 3
7 5 2
8 5 3
9 6 1
10 6 2
11 6 3
12 7 3
13 9 3
14 9 2
15 9 1
and using this query:
SELECT itemId, GROUP_CONCAT( keywordId ORDER BY keywordId ) AS crpcnct, COUNT( itemId )
FROM `temporary_table_123`
GROUP BY 1
HAVING crpcnct = '1,2,3'
I can get the value that you wanted:
itemId crpcnct count(itemId)
4 1,2,3 3
6 1,2,3 3
9 1,2,3 3
To achieve this, all you have to do is build the keywordID you want to use:
$keywordIds[] = $results['keywordId'];
and then sort accending
sort($keywordIds);
the last step is, supply this array into query:
SELECT itemId, GROUP_CONCAT( keywordId ORDER BY keywordId ) AS crpcnct, COUNT( itemId )
FROM `temporary_table_123`
GROUP BY 1
HAVING crpcnct = '" . implode(",", $keywordIds) . "'
There you have it.
SELECT ItemID FROM table2 WHERE keywordID IN (SELECT keywordID FROM table1)
This might work. I'll have to create local copies of your tables to see for sure, though.
$array_names = array();
foreach($explode_tags as $k=>$explode_tag){
$array_names[] = 'name = ' . $db->Quote(trim($explode_tag));
}
$sql = "SELECT ItemID FROM table_2 WHERE keywordID IN (SELECT keywordID FROM table_1 WHERE " . implode(' OR ', $array_names) .")";
$db->setQuery($sql);
$results = $db->loadResultArray();
I have a table with two columns:
ID1 | ID2
---------
1 | A
3 | V
1 | C
4 | B
5 | Q
1 | S
And I want to be able to find out if any row has, say ID1 = 5 and ID2 = Q , and return a true / false value.
Yes, Of course
SELECT * FROM table where ID1='5' and ID2='Q'
PHP (I am just guessing this backend)
$query = "SELECT * FROM table where ID1='5' and ID2='Q'";
$result = mysql_query($query);
if(mysql_num_rows($result)) { return true; }
else { return false; }
1 means TRUE for mysql
SELECT 1
FROM your_table
WHERE ID1 = 5 AND ID2 = 'Q'
for example this?
SELECT 1
FROM TABLE
WHERE ID1 = 5 AND ID2 = 'Q'
Efficient query for your purpose (faster than other examples):
SELECT 1 FROM table where ID1='5' and ID2='Q' LIMIT 1
PHP sample:
<?php
$query = "SELECT 1 FROM table where ID1='5' and ID2='Q' LIMIT 1";
echo bool(mysql_num_row(mysql_query($query)));
?>