I have a mySQL table like this example:
ID | ProcID | Value
1 | 6 | one
1 | 7 | two
1 | 8 | three
1 | 9 | four
2 | 6 | one
2 | 7 | two
2 | 8 | three
2 | 9 | four
I need to get only the ID #1 and the value from the ProcID 6 and 7.. how can I do with only one PHP query?
I already did with two separate queries but I'm sure is possible with only one, maybe with some parameters such "ON" or "IN" but I didn't find anything!
Try this:
SELECT *
FROM table
WHERE ID = 1 AND ProcID IN (6,7)
Sample result:
ID | ProcID | Value
--------------------
1 | 6 | one
1 | 7 | two
If you want to select the only ID of 1 from ProcID use :
SELECT * FROM `table` where `ID` = '1' AND `ProcID` = '6' AND `ProcID` = '7'
Related
I have a table which has a sort_no column and the sort values are belonging to q_id which corresponds to question id. But, it does not include proper sorting values. Sometimes, the sort numbers are being repeated for different records with the same q_id. I have to refactor this table with unique sort numbers for each question.
This is a sample data I already have:
id | name | sort_no | q_id
-------------------------------
1 | val_1 | 1 | 21
2 | val_2 | 2 | 21
3 | val_3 | 1 | 32
4 | val_4 | 3 | 21
5 | val_5 | 2 | 32
6 | val_6 | 2 | 32
7 | val_7 | 1 | 25
8 | val_8 | 1 | 21
9 | val_9 | 1 | 21
-------------------------------
This is what it should be:
id | name | sort_no | q_id
------------------------------
1 | val_1 | 1 | 21
2 | val_2 | 2 | 21
3 | val_3 | 1 | 32
4 | val_4 | 3 | 21
5 | val_5 | 2 | 32
6 | val_6 | 3 | 32
7 | val_7 | 1 | 25
8 | val_8 | 4 | 21
9 | val_9 | 5 | 21
-------------------------------
Actually, I can fetch the records and put them in a loop and update it by a loop. But, as you know, it takes time and resource. The table is huge with millions of records.
I was wondering if I could do it directly in MySQL with a nested query.
I have no idea about the query.
Have anybody experienced this before?
update test5
set sort_no=#srt:=if(#grp=q_id,#srt+1,1),
q_id=#grp:=q_id
where (0,0)=(select #grp:=0,#srt:=0)
order by q_id, `name`
Set needed 'order by'. First column in 'order by' must be "q_id".
NOTE: before running this query, the update safe mode should be disabled (if not by default):
SET SQL_SAFE_UPDATES = 0;
ORDER BY doesn't have to be just a field or a list of fields. it can be arbitrary expressions too, e.g.
ORDER BY foo = 42 DESC, foo
would put all records with 42 FIRST, then the rest of them in regular numeric order.
If you can come up with some expression that calculates a value that fits your desired sort order, then...
ORDER BY somefunc(...)
would also work. What matters is the value returned by the function, which is what the DB will use to do the actual sorting.
I am sorry for the vague title but I really cannot make this any more specific with my English.
I have this table called posts__tags that I use to associate post ids and tag ids with the following columns
record_id, post_id and tag_id. Here is an example sample
record_id | post_id | tag_id
1 | 1 | 1
2 | 1 | 2
3 | 1 | 3
4 | 2 | 2
5 | 3 | 1
6 | 3 | 3
7 | 4 | 2
8 | 4 | 3
9 | 4 | 4
I have a php function that has an array of tag_ids as a parameter. Lets say for example that my parameter is the array(2,3). I need to select all the post_id's that are associated with tag_id = 2 AND tag_id = 3.
In the above example I should get the 1 and 4 post_id's since only those are associated with both 2 and 3 tag_id 's.
What is the best way to achieve that with a MySQL query?
Thanks in advance
select post_id from posts__tags where tag_id in (1,2) group by post_id having count(*)=2
Try this
select distinct post_id from table_name where tag_id in (2,3)
I have a table like this:
// mytable
+----+--------+-------------------+
| id | word | numbers |
+----+--------+-------------------+
| 1 | hello | 1<br>2<br>3<br> |
| 2 | how | 12<br>15<br> |
| 3 | are | 453<br>1<br> |
| 4 | you | 3<br>33<br>453<br>|
+----+--------+-------------------+
And I want this output:
// mynewtable
+----+--------+---------+
| id | word | numbers |
+----+--------+---------+
| 1 | hello | 1 |
| 2 | hello | 2 |
| 3 | hello | 3 |
| 4 | how | 12 |
| 5 | how | 15 |
| 6 | are | 453 |
| 7 | are | 1 |
| 8 | you | 3 |
| 9 | you | 33 |
| 10 | you | 453 |
+----+--------+---------+
I can do that using PHP. First I have to fetch all rows and implode them by <br> then insert them again. But now I want to know, is there any better solution? (specially using pure MySQL)
Try this solution through query
SELECT #row := #row + 1 as row, t.word, t.number FROM (
SELECT
word,
SUBSTRING_INDEX(SUBSTRING_INDEX(numbers, '<br>', n.digit+1), '<br>', -1) number
FROM split_rec
INNER JOIN
(SELECT 0 digit UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3) n
ON LENGTH(REPLACE(numbers, '<br>' , '')) <= LENGTH(numbers)-n.digit
ORDER BY id, n.digit
) AS t, (SELECT #row := 0) r
WHERE t.number <> ''
Output
row word number
1 hello 1
2 hello 2
3 hello 3
4 how 12
5 how 15
6 are 453
7 are 1
8 you 3
9 you 33
10 you 453
While I think that it might be possible using stored procedures, MySQL variables and a liberal use of LIKE "%...%"statements, it would be an order of magnitude more complex than a PHP solution.
Furthermore, I think it is not possible at all if you don't have access to stored procedures as you would need dynamically create new INSERT statements based on what you find in that last column.
TL;DR: No, make yourself happy and use PHP.
I have this 2 table in database in sql
TABLE1:
USER id | state| TYPE | time |
1 | 1 | 1 | time |
2 | 1 | 2 | time |
3 | 1 | 2 | time |
4 | 1 | 2 | time |
5 | 0 | 1 | time |
6 | 0 | 1 | time |
TABLE2:
id |USER id| run
1 | 3 | 7
2 | 1 | 5
3 | 1 | 5
4 | 4 | 8
5 | 2 | 6
6 | 2 | 6
7 | 3 | 7
8 | 3 | 7
9 | 3 | 7
10 | 3 | 7
11 | 2 | 6
12 | 4 | 8
13 | 4 | 8
14 | 1 | 5
15 | 2 | 6
16 | 2 | 6
17 | 5 | 9
18 | 4 | 8
I am printing this
SELECT * FROM TABLE1 WHERE state != 0
it will print row this way
USER ID 1
USER ID 2
USER ID 3
USER ID 4
But I want to ascending this by count
WHERE, Count = num of row of TABLE2
Where user id=1 or 2 or..N
Here:
count of USER id 1 = 3
count of USER id 2 = 5
count of USER id 3 = 5
count of USER id 4 = 4
Now i want to ascending
Table 1 where high count to low count from table 2 and high run to low run
USER ID 3
USER ID 2
USER ID 4
USER ID 1
Using PHP AND MYSQLI
Please help me
select t1.id, count(t2.id) as t2_count
from table1 t1
left join table2 t2 on t1.`user id` = t2.`user id`
group by t1.id
order by t2_count desc
I posted another question that is similar.
So still having more problems.
This is the case, I want to be able to update the column position.
following the example below. here is before the update
+----+--------------+----------+
| id | question | position |
+----+--------------+----------+
| 1 | Question 1 | 1 |
| 2 | Question 2 | 2 |
| 3 | Question 3 | 3 |
| 4 | Question 4 | 4 |
+----+--------------+----------+
So If I update the row id = 2 the column position from 2 to 4, I want to be organized as the following. here is after the update
+----+--------------+----------+
| id | question | position |
+----+--------------+----------+
| 1 | Question 1 | 1 |
| 2 | Question 2 | 4 |
| 3 | Question 3 | 2 |
| 4 | Question 4 | 3 |
+----+--------------+----------+
I tried many thing and nothing works.
So if anyone could help me will be very apprecciated.
Thanks a lot.
You want to do something like this:
update t join
(select position from t where id = 2) t1
on t.position >= t1.position
set position = (case when id = 2 then 4 else position - 1 end)
That is, subtract "1" from all positions greater than the one with id = 2. And set that position to "4".