Here is my table:
// table
+----+------+------+
| id | col1 | col2 |
+----+------+------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 1 | 3 |
| 4 | 2 | 1 |
| 5 | 2 | 2 |
| 6 | 3 | 1 |
| 7 | 3 | 2 |
| 8 | 3 | 3 |
| 9 | 3 | 4 |
| 10 | 3 | 5 |
+----+------+------+
Now I want to search in both col1 and col2. Something like this:
select * from table where col1,col2 IN (1,2);
And I want this output:
+----+------+------+
| id | col1 | col2 |
+----+------+------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 1 | 3 |
| 4 | 2 | 1 |
| 5 | 2 | 2 |
| 6 | 3 | 1 |
| 7 | 3 | 2 |
+----+------+------+
Well, My problem is on this part: ... where col1,col2 IN (1,2). How can I solve it?
Note: I can do that like this: ... where col1 IN (1,2) or ,col2 IN (1,2). But this this way, I have to create two separate index on each column. While I want a query which need to a group-index like this: KEY NameIndex (col1, col2)
You want this, correct?
WHERE col1 IN (1,2)
OR col2 IN (1,2)
If so, turn the OR into a UNION. (This is a common optimization trick.)
( SELECT ... WHERE col1 IN (1,2) )
UNION DISTINCT -- since there are likely to be dups
( SELECT ... WHERE col2 IN (1,2) );
And provide the optimal index for each SELECT:
INDEX(col1),
INDEX(col2)
A composite index of those two columns will not suffice.
(Appologies -- this is probably a summary of best of the many disjointed comments.)
Related
Suppose I have a table t1 like
mysql> select * from t1;
+------+-------+------+
| id | level | gap |
+------+-------+------+
| 1 | 6 | 50 |
| 1 | 5 | 10 |
| 2 | 5 | 12 |
| 2 | 5 | 10 |
| 3 | 8 | 4 |
| 3 | 9 | 1 |
| 3 | 9 | 3 |
| 3 | 7 | 2 |
+------+-------+------+
I want to insert a row (3,6,7) into here.I mean it is below in first 5 row.
Is it possible in mysql?
Just do
INSERT INTO t1 (id, level,gap) VALUES (3,6,7)
Records in a table do not have a prescribed order. The order has to be defined during a SELECT by supplying a suitable ORDER BY clause.
So, if you want the new record to be listed in 5th position use ORDER BY id, level.
I have a table like this:
| ID1 | ID2 |
------------------
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 1 |
| 5 | 1 |
| 6 | 1 |
| 7 | 1 |
| 8 | 1 |
| 9 | 1 |
| 10 | 1 |
| 11 | 1 |
| 12 | 1 |
| 13 | 1 |
| 1 | 2 |
| 2 | 2 |
| 5 | 2 |
| 1 | 4 |
| 9 | 4 |
Some of the ID1s have both the 1 and 4 values like this:
And I would like to return the ID1s like this:
| ID1 |
----------
| 1 |
| 9 |
I have tried some basic queries but Im not even close.. I just cannot get this to work even remotely and I need some serious help with this one.
Try this:
select id1
from yourtable
where id2 in (1,4)
group by id1
having count(distinct id2) = 2
Demo Here
If ID1 is supposed to have only one unique value then the following query would work for you:
SELECT
ID1
FROM your_table
GROUP BY ID1
HAVING COUNT(DISTINCT ID2) > 1;
EDIT: May be you are looking for those entries having ID2 as 1 and 4 both.
SELECT
ID1
FROM your_table
WHERE ID2 IN (1,4)
GROUP BY ID1
HAVING COUNT(DISTINCT ID2) = 2;
Note: It would have been better if you clarify what combination of ID1 & ID2 is supported in your table.
I have two tables - Table 1 of ints and Table 2 of strings.
Table 1:
+-------+-------+-------+-------+-------+
| | col A | col B | col C | col D |
+-------+-------+-------+-------+-------+
| row 1 | 1 | | | |
| row 2 | | 2 | | |
| row 3 | 8 | 3 | | |
| row 4 | 9 | | 4 | |
+-------+-------+-------+-------+-------+
Table 2:
+-------+-------+--------------+
| | col A | col B |
+-------+-------+--------------+
| row 1 | 1 | dog |
| row 2 | 2 | cat |
| row 3 | 3 | zebra |
| row 4 | 4 | donkey |
| row 5 | 8 | horse |
| row 6 | 9 | honey badger |
+-------+-------+--------------+
Is there a SQL query that will return the following?
+-------+--------------+-------+--------+-------+
| | col A | col B | col C | col D |
+-------+--------------+-------+--------+-------+
| row 1 | dog | | | |
| row 2 | | cat | | |
| row 3 | horse | zebra | | |
| row 4 | honey badger | | donkey | horse |
+-------+--------------+-------+--------+-------+
At the moment I am SELECT * IN Table_1.
Then querying Table_2 six times to get the result. Is there a more elegent way?
I do want to use SELECT * -- I do not want to specify the table headings in the query (because there are 50+ table headings).
The only way to do lookups for all columns is by mentioning all the columns in the SQL query.
This could be done with one left join for each column, or with correlated subqueries:
SELECT (SELECT colB FROM Table2 WHERE colA = Table1.colA) AS colA,
(SELECT colB FROM Table2 WHERE colA = Table1.colB) AS colB,
(SELECT colB FROM Table2 WHERE colA = Table1.colC) AS colC,
...
FROM Table1
You cannot use SELECT *. If you do not know the columns of Table1, you can read them from the database, and construct the query dynamically.
Suppose I have such a table:
+-----+---------+-------+
| ID | TIME | DAY |
+-----+---------+-------+
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 3 | 1 |
| 1 | 1 | 2 |
| 2 | 2 | 2 |
| 3 | 3 | 2 |
| 1 | 1 | 3 |
| 2 | 2 | 3 |
| 3 | 3 | 3 |
| 1 | 1 | 4 |
| 2 | 2 | 4 |
| 3 | 3 | 4 |
| 1 | 1 | 5 |
| 2 | 2 | 5 |
| 3 | 3 | 5 |
+-----+---------+-------+
I want to fetch a table which represents 2 IDs which got the largest sum of TIME within the last 3 days (means from 3 to 5 in a DAY column)
So the correct result would be:
+-----+---------+
| ID | SUM |
+-----+---------+
| 3 | 9 |
| 2 | 6 |
+-----+---------+
The original table is much larger and more complex. So i need a generic approach.
Thanks in advance.
And so I just learned that MySQL used LIMIT instead of TOP...
fiddle
CREATE TABLE tbl (ID INT,tm INT,dy INT);
INSERT INTO tbl (id, tm, dy) VALUES
(1,1,1)
,(2,2,1)
,(3,3,1)
,(1,1,2)
,(1,1,1)
SELECT ID
,SUM(SumTimeForDay) SumTimeFromLastThreeDays
FROM (SELECT ID
,SUM(tm) SumTimeForDay
FROM tbl
GROUP BY ID, dy
HAVING dy > MAX(dy) -3) a
GROUP BY id
ORDER BY SUM(SumTimeForDay) DESC
LIMIT 2
select t1.`id`, sum(t1.`time`) as `sum`
from `table` t1
inner join ( select distinct `day` from `table` order by `day` desc limit 3 ) t2
on t2.`da`y = t1.`day`
group by t1.`id`
order by sum(t1.`time`) desc
limit 2
I'm working on a huge dataset, with a table that looks like this :
+----+---------+--------+--------+
| id | otherid | value1 | value2 |
+----+---------+--------+--------+
| 1 | 1 | 2 | 5 |
| 1 | 1 | 4 | 8 |
| 1 | 2 | 3 | 6 |
| 2 | 123 | 1 | 4 |
+----+---------+--------+--------+
I need to multiply value1 and value2 for each row, and sum values per id and otherid. A result table might be:
+----+---------+-----+
| id | otherid | sum |
+----+---------+-----+
| 1 | 1 | 42 | ((2*5)+(4*8))
| 1 | 2 | 18 | (3*6)
| 2 | 123 | 4 | (1*4)
+----+---------+-----+
My question is if it is possible to avoid subqueries to do this, I only found solutions that used them.
Thanks!
it's easy.
SELECT id,
otherid,
SUM(value1*value2) AS sum
FROM your_table
GROUP BY id, otherid;
Try Below Query
SELECT ID,otherid ,SUM(value1 * value2) sum
FROM TABLE1
GROUP BY ID,otherid