I have a mysql table which include product and attribute relations,
id productid attributeid arributevalueid
18 521 12 36
17 521 11 43
16 521 9 16
29 522 18 168
28 522 17 138
27 522 16 115
26 522 15 71
25 522 12 36
24 522 11 48
23 522 9 19
i got a problem when i write a sql query, i'm trying to to filter product which has all attributes,
ex - if i pass attribute value 16 and 36 the product matched is 521
i test a mysql query below
$nweryt=mysql_query('SELECT * FROM
tbl_proattrconnect
WHERE
tbl_proattrconnect.attroid=9 AND tbl_proattrconnect.attrvalid=16
AND tbl_proattrconnect.attroid=12 AND tbl_proattrconnect.attrvalid=36');
echo mysql_num_rows($nweryt);
But this cant get the results and show 0, can anyone help me please, thanks a lot
Please have a try with this:
SELECT
productid
FROM
tbl_proattrconnect
GROUP BY productid
HAVING SUM(attributevalueid IN (16, 36)) >= 2 /*here you specify after the >= how many attributes you have in IN()*/
AND SUM(attributevalueid BETWEEN x and y) >= 1 /*here you just always leave it at >= 1*/
AND SUM(attributevalueid BETWEEN z and w) >= 1 /*feel free to add as much checks as necessary.*/
This uses a little trick. attributevalueid IN (16, 36) returns true or false, 1 or 0.
Alternatively you can self join the table, but this may have a bit worse performance and can be quite clumsy if you have to check for more attributes. You have to self join the table once for each attribute.
see it working live in an sqlfiddle
You need to join the table to itself to make this happen. For example:
SELECT t1.productid
FROM tbl_proattrconnect t1
JOIN tbl_proattrconnect t2 ON t1.productid = t2.productid
WHERE
(t1.attroid = 9 AND t1.attrvalid = 16)
AND
(t2.attroid = 12 AND t2.attrvalid = 36)
You have a mistake in your query because you are using an AND operator and not OR.
Remember:
The AND operator displays a record if both the first condition AND the second condition are true.
The OR operator displays a record if either the first condition OR the second condition is true.
Read this info http://www.w3schools.com/sql/sql_and_or.asp
$nweryt=mysql_query('SELECT * FROM tbl_proattrconnect
WHERE
tbl_proattrconnect.attroid=9 AND tbl_proattrconnect.attrvalid=16
OR
tbl_proattrconnect.attroid=12 AND tbl_proattrconnect.attrvalid=36');
echo mysql_num_rows($nweryt);
Also can write the same query but shorter:
SELECT * FROM
tbl_proattrconnect
WHERE
tbl_proattrconnect.attroid IN(9,12) AND tbl_proattrconnect.attrvalid IN(16,36);
Maybe you meant to use OR rather than AND? Try again, this will display 521 and 522
$nweryt=mysql_query('SELECT * FROM
tbl_proattrconnect
WHERE
tbl_proattrconnect.attroid=9 AND tbl_proattrconnect.attrvalid=16
OR tbl_proattrconnect.attroid=12 AND tbl_proattrconnect.attrvalid=36
order by productid'
);
Related
id - idProduct - idFilter
65 - 494 - 3
66 - 495 - 3
67 - 497 - 17
68 - 500 - 17
69 - 500 - 3
How I can get all idProducts who have idFilter 17 AND 3. In this case this is only product with id - 500
I try something like this:
$query="SELECT idProduct FROM tablename WHERE (idFilter = '3') OR (idFilter = '17') GROUP BY idProduct";
I know it's stupid but I don't know any idea?
Thank you
In case if you need to compare multiple row values on a single column, Such that the value should satisfy with an ANS condition.
Then you can do the following trick to achieve that:
SELECT distinct(idProduct) as idProduct,count(*) AS total FROM tablename
WHERE idFilter IN (17,3) GROUP BY idProduct
HAVING total = 2;
//Having will force result to have both 17 and 3 else 17 or 3 alone will be in the result
If using php variable for the above ids, you can try the following
$inArray = array(17,3);
$sql = "SELECT distinct(idProduct) as idProduct,count(*) AS total FROM tablename
WHERE idFilter IN (".implode(',',$inArray).") GROUP BY idProduct
HAVING total = '".count($inArray)."'";
I have a problem trying to work out some logic when trying to sort. Even went back to Excel to work out simple logic.
What I have is :-
ColA ColB
100022 33
100022 36
100024 28
100024 32
100024 41
100024 52
100026 38
100038 28
100038 42
100038 48
100038 59
What I want to do is sort by Column B (heightest to lowest) BUT I dont want Column A to have the same values next to each other.Sorting the above will have 33 & 36 (with 100022) next to each other (and 42&48) with 100038 next to each other. As long as Column B is sorted as best it can from heightest to lowest and Column A's values are never next to each other (Unless absolutely necessary) I'm happy.
Ideally I am after a logic to end up with something like this ..
100024 28
100038 28
100022 33
100024 32
100022 36
100026 38
100038 42
100024 41
100038 48
100024 52
100038 59
Any help or ideas would be greatly appreciated.
Thank you
Brad
If you want to enforce that no two values from ColA appear adjacent, then one option would be to assign a row number to each value in a given ColA block, and then order to ensure that adjacent values do not occur.
SET #row_number = 0;
SET #ColA = NULL;
SELECT t.ColA, t.ColB
FROM
(
SELECT
#row_number:=CASE WHEN #ColA = ColA THEN #row_number + 1 ELSE 1 END AS rn,
#ColA:=ColA AS ColA,
ColB
FROM
yourTable
ORDER BY ColA, ColB DESC
) t
ORDER BY t.rn, t.ColB DESC
You mentioned in your question that wanted ColB sorted from highest to lowest, which I have done. If you want ascending ordering, just remove the DESC from the above query.
Output:
Demo here:
Rextester
Like this a result :
75 Ansari 5 10
88 Koodoo 4 0
90 Koodoo 14 0
83 Koodoo 5 0
82 Koodoo, 6 0
81 Koodoo 4 0
79 Koodoo 5 0
74 Savage 1 0
80 Strike 2 36
87 Strike 4 15
78 Sullivan 3 15
77 Sullivan 2 0
I would like to get the total for each member for the last 2 columns (Hours and Minutes).
My query look like that :
SELECT
*
FROM
$tbl_name
ORDER BY
player
If someone would have a quick fix for that I would appreciate it.
Probably you want something like this (but you didn't provide a schema so I'm guessing on the field names).
SELECT `id`, `player`, SUM(`hours`) as hours, SUM(`minutes`) as minutes FROM `$tbl_name` GROUP_BY `player`;
You need to GROUP BY the user's name and SUM the totals for the hours and minutes columns.
A Simple example of this is:
SELECT `id`,
`user`,
SUM(`hours`) as tot_hours,
SUM(`mins`) as tot_mins
FROM `test`
GROUP BY `user`;
You can see this example run at this SQLFiddle
Note: With that fiddle, I removed the extra comma in this line (I assumed it was a typo)
82 Koodoo, 6 0
If that comma is supposed to be there, you just need to adjust that one insert statement to add the comma within the quotes of the name. That will adjust your query outcome as Koodoo, won't group with the other Koodoo values.
Okay guys, I will try to share my guestion understandable. So I have 2 tables like below:
ALL Id from TABLES are Autoincrement.
Number
id number
10 100
11 102
12 105
13 106
subnumber
id number_id subnumber
52 10 10
53 11 15
54 13 40
You see some numbers (not all) have subnumbers. I mean:
From table number the number where id = 13 has subnumber and it is equal to 40.
I want to save this table in another table.
$sql1=mysql_query('SELECT number FROM number');
$while(fetch1=mysql_fetch_array($sql1))
{
mysql_query('
INSERT INTO `save_number` (number)'
VALUES ('.$fetch1['number'].');
);
}
save_number
id number
100 100
101 102
102 105
103 106
Now i gonna save table (subnumber) in another table (save_subnumber)
$sql2=mysql_query('SELECT number_id, subnumber FROM subnumber');
$while(fetch2=mysql_fetch_array($sql2))
{
mysql_query('
INSERT INTO `save_subnumber` (number_id, subnumber)'
VALUES ('.$fetch2['number_id'].', '.$fetch2['number'].');
);
}
save_subnumber
id number_id subnumber
60 10 10
61 11 15
62 13 40
So, you see the number_id FROM save_subnumber is not equal to new inserted id FROM save_number. I would be appreaciated if anyone help me. By the way I am still using mysql_query. I can not find time to improve my SQL to PDO :)
Your first query can be changed to
INSERT INTO
save_number (number)
SELECT
number
FROM
number
This will save you using PHP to iterate through rows and will be faster. A lot.
Having this in mind your second query would be
INSERT INTO
save_subnumber (number_id, subnumber)
SELECT DISTINCT
sn.id, s.subnumber
FROM
saved_number AS sn
CROSS JOIN
number AS n
USING (number)
INNER JOIN
subnumber AS s
ON
n.id = s.number_id
If you just want to copy one table in another why don't you use something like this
INSERT INTO save_number SELECT * FROM number;
I want a resultset for this table:
ID Number_of_posts Number_of_user
1 100 21
2 23 34
as
ID Number_of_posts Number_of_user Number_of_posts_AND_Number_of_user
1 100 21 178
2 23 34 178
-----------------------------------------------
123 55
Is it possible to get the sum of two colums as another column/ as output in mysql?
To get cross-tab totals (horizontal and vertical):
select id,
number_of_posts as p,
number_of_users as u,
number_of_posts+number_of_users as p_and_u
from tbl
union all
select 99999 as id,
sum(number_of_posts) as p,
sum(number_of_users) as u,
sum(number_of_posts+number_of_users) as p_and_u
from tbl
order by 1
This will give you:
id p u p_and_u
----- --- --- -------
1 100 21 121
2 23 34 57
99999 123 55 178
You're complicating your query needlessly and using more memory that you have to. Pull the records in one query, then make another query to get the aggregates.
I know it doesn't answer your question, but it's what you should be doing instead. =)
SELECT id, number_of_posts, number_of_user,
(
SELECT SUM(number_of_posts + number_of_user)
FROM mytable
)
FROM mytable
SELECT SUM(Number_of_posts), SUM(Number_of_user) FROM table;
SELECT *,
(SELECT SUM(Number_of_posts) + SUM(Number_of_user) FROM TABLE) AS total
FROM table;
(Edit: Didn't originally notice it was the total total in the last column.)
Does MySQL support ROLLUP?
SELECT id,
SUM(Number_of_posts) Number_of_posts,
SUM(Number_of_user) Number_of_user,
SUM(Number_of_posts) + SUM(Number_of_user) Number_of_posts_AND_Number_of_user
FROM table
GROUP BY ROLLUP(id)
Edit: based on a quick search, in MySQL the last line might be GROUP BY id WITH ROLLUP.