I have a query:
<?php
$results = $dbConn->select("SELECT entryA, entryB FROM table");
/**
Displays the rows on $results (entryA, entryB)
1 7
8 5
4 3
5 8
7 1
3 4
**/
$results = $dbConn->select("SELECT entryA, entryB FROM table ORDER BY ?");
/**
The correct output must be: (entryA, entryB)
1 7
7 1
8 5
5 8
4 3
**/
?>
How can i possible to order two columns that equal/match to each other ids in a different row?
Thanks in advance.
Yes, it is. For example
SELECT entryA, entryB FROM table ORDER BY entryA*entryA+entryB*entryB
-in my samples I'm supposing you have both pair variants, i,e {1,7} and {7,1}, for example.
This will group same pairs independent of elements order, but you may wish to have additional order condition - then simply add it to ORDER BY clause
Related
Here is the table structure. I want to fetch all the id_product which have value 2 and 601(here id_product = 5). If i use OR all the records will be populated which is not necessary.
id_product attribute_id Value
5 2 2
6 2 2
8 2 601
6 2 601
5 3 601
8 3 32
6 3 41
Any help would be appreciated. I don't want to use sub query :-p
You can use a group by query:
select
id_product
from
tablename
where
attribute_id=2 and values in (2,601)
group by
id_product
having
count(*)=2
this will select all products that have (attribute_id=2 and value=2) or (attribute_id=2 and value=601) in two different rows, and then it will count the number of rows returned and select only products that have two rows (one with value 2 and one with value 601).
Another option (it's not too clear from your question) is to use this where clause instead of the one on the query above:
where
(attribute_id=2 and value=2) or
(attribute_id=3 and value=601)
You can use this query in your case:
SELECT * FROM nameTable WHERE Values IN (2,601) and attribute_id = 2
I can do it with a loop or an alternate solution, but this would mean time consuming (on page loading).... So I would like to know if there is a 'one query' solution.
I have a table containing 4 columns:
id class day hour
1 9b3 1 3
2 9b4 1 3
3 9b5 1 3
4 9b3 1 5
5 9b4 2 6
6 9b5 2 6
7 9b4 4 7
8 9b3 3 6
9 9b4 3 6
10 9b5 3 6
What I need is to recover the day and hour matching all three classes 9b3, 9b4, 9b5 at the same day and hour.
In the example above, the result should be:
day hour
1 3
3 6
Try this:
SELECT day,hour
FROM yourTableName
WHERE class IN ('9b3','9b4','9b5')
GROUP BY day,hour
HAVING COUNT(class) = 3;
sqlfiddle demo
also that works...
SELECT day,hour
FROM Table1
WHERE (class = '9b3' or class = '9b4' or class = '9b5')
GROUP BY day,hour
HAVING COUNT(class) = 3;
SELECT day,hour FROM table
WHERE class IN('9b3','9b4','9b5')GROUP BY day HAVING COUNT(class)=3;
This can be achieved simple by using SQL statement syntax depending on the action that you want to perform. Picking the raw data can be done like this:
SELECT * FROM YourTable
WHERE day='1' and hour='3'
I have the string $niveis with the following values (example):
3,8,10
Using this string, I need to count the number of rows in table content, where the access matches any of these 3 values.
For example, if table content is:
id access
1 2
2 3
3 3
4 7
5 8
6 9
7 10
8 10
Should return 5.
I tried the following query in PHP:
$query="SELECT count(id) FROM #__content WHERE access =$niveis";
But it isn't working, can anyone help?
You can use IN keyword
SELECT COUNT(id)
FROM #__content
WHERE access IN ($niveis)
You can use find_in_set():
select count(id)
from #__content
where find_in_set(access, $niveis) > 0;
This question already has an answer here:
mysql don't count row twice if column data is duplicated
(1 answer)
Closed 9 years ago.
i have a a table called ptb_profile_views;
id | profile_id | viewed_profile | date
1 1 5
2 1 5
3 1 5
4 2 5
5 3 5
ok so in this example users 1,2 ans 3 are checking out user 5's profile,
the mysql i've got here is suppose to count the number of views a user has got, but where user 1 has checked out user 5 several times i do not want to return duplicate rows,
so instead of saying user 5 has been viewed 5 times, they will have only actually been viewed 3 times because i only want to count distinct values.
heres my mysql can someone please help me:
function check_profile_views() {
global $connection;
global $_SESSION;
$query = "SELECT COUNT(profile_id) FROM ptb_profile_views WHERE viewed_profile_id=".$_SESSION['user_id']." AND profile_id!='0'";
$check_profile_views_set = mysql_query($query, $connection);
confirm_query($check_profile_views_set);
return $check_profile_views_set;
}
the php:
$check_profile_views_set = check_profile_views();
while ($views = mysql_fetch_array($check_profile_views_set)) {
?>
<? echo"". $views['COUNT(profile_id)'] ."";?> viewed your profile
just add DISTINCT
SELECT COUNT(DISTINCT profile_id) totalCOUNT FROM ptb_profile_views...
and echo the alias,
$views['totalCOUNT']
SQLFiddle Demo
I have a table, which consists of 3 fields:
id
name
status
Every time I get the results, it should give me 5 names whose status = 1.
Suppose the db contains following:
id name status
1 A 1
2 B 1
3 C 0
4 D 1
5 E 0
6 F 0
7 H 1
8 I 1
9 J 1
10 K 1
11 L 1
12 M 0
1st time, fetch should return: A,B,D,H,I (5 records)
2nd time, fetch should return: J,K,L,A,B (5 records)
UPDATE: I don't want typical pagenation. Consider I have 12 available names from A1 to A12. The first fetch should return A1-A5, second fetch A6-A10 and third fetch A11, A12, A1, A2, A3. So when I reach the end, I need to get records starting from the first to fill the 5 slots.
i am doing it in php with mysql
This looks like some sort of job allocation script?
You need 2 things:
the highest ID returned last time the script was run (lastID)
a number larger than the maximum ID in the table (bigNum)
Then you can write your query as
SELECT
id, name
FROM
table
WHERE
status=1
ORDER BY
(bignum + id) MOD (bigNum + lastID + 1)
LIMIT 5
Shazaam!
Keep track of the ids of the records returned, and for the following queries do:
select top 5 *
from (
select top 5 *
from MyTable
where status = 1
and id not in (1,2,4,7,8)
order by name
union
select top 5 *
from MyTable
where status = 1
order by name
) a
$q = mysql_query("SELECT name FROM table WHERE status = 1 LIMIT 5);
while ($row = mysql_fetch_row($q))
{
.... //first 5
}
$q = mysql_query("SELECT name FROM table WHERE status = 1 LIMIT 5,5);
while ($row = mysql_fetch_row($q))
{
.... //second 5
}
this uses the offset functionality of mysql- think of it as pagination for your results.