I'm looking for a way to search a table values from the results of a query on another table :
SELECT entry_id FROM FEEDENTRYSTATUSES WHERE starred > 0 ;
+----------+
| entry_id |
+----------+
| 1036 |
| 1059 |
+----------+
2 rows in set (0.00 sec)
SELECT url from FEEDENTRIES WHERE id = 1036 ;
+---------------------+
| url |
+---------------------+
| https://google.com/ |
+---------------------+
1 row in set (0.00 sec)
So I can get a list of IDs from the FEEDENTRYSTATUSES table.
I can retrieve value from second table manually by giving the value 1036.
But I would like to search in table FEEDENTRIES from values returned by first SELECT.
Is here a way to do so ?
Any help very much appreciated.
Thanks a lot
You can use IN operator and subquery to achieve that:
SELECT url from FEEDENTRIES
WHERE id IN (SELECT entry_id FROM FEEDENTRYSTATUSES WHERE starred > 0);
Or join the tables and filter the data that you need:
SELECT fe.url from FEEDENTRIES fe
INNER JOIN FEEDENTRYSTATUSES fes ON fe.id = fes.entry_id
WHERE fes.starred > 0;
Related
I have two tables:
Table 1: qtrade
qtrade columns
qtrade values
Table 2: qsale
qsale columns
qsale values
These two table have common "tid" which is unique trade id. I need to get tid's with their qsale values if it is available. So, i tried to LEFT JOIN method like this:
'SELECT *
FROM `qtrade`
LEFT JOIN `qsale` ON qtrade.tid = qsale.tid'
The query retrieves joined data, but for tid=11 there is no qsale record, so it retrieves NULL valeus as expected, but also overrides tid with NULL value as not expected. It gets tid NULL.
I have serached that and found COALESCE trick. It might work, but i would write down all column names in qtrade and qsale, these are around 32 columns. Too long. If there any trick to overcome this issue. I think 'SELECT *, COALESCE(qsale.tid, qtrade.tid) tid' will not work. Meaning only coalesce tid, and get all column data. Is there any other way ?
What you describe does work.
Demo:
mysql> create table qtrade (tid int);
Query OK, 0 rows affected (0.01 sec)
mysql> create table qsale (tid int);
Query OK, 0 rows affected (0.01 sec)
mysql> insert into qtrade set tid=42;
Query OK, 1 row affected (0.01 sec)
mysql> SELECT *
-> FROM `qtrade`
-> LEFT JOIN `qsale` ON qtrade.tid = qsale.tid;
+------+------+
| tid | tid |
+------+------+
| 42 | NULL |
+------+------+
1 row in set (0.00 sec)
mysql> SELECT *, COALESCE(qsale.tid, qtrade.tid) AS tid
FROM `qtrade` LEFT JOIN `qsale` ON qtrade.tid = qsale.tid;
+------+------+------+
| tid | tid | tid |
+------+------+------+
| 42 | NULL | 42 |
+------+------+------+
1 row in set (0.00 sec)
MySQL query result sets allow multiple columns to have the same name.
But the problem arises when you have a client that fetches the results into an associative array or hashmap, which only allows one entry per name.
In that case, the only alternative is to change the client code to fetch results into an ordinal array instead of an associative array, and then reference the columns of the result by position instead of by name.
I never use SELECT * in production code anyway. Just write out the columns. If typing 32 column names is the bottleneck in your programming productivity, then you're doing it wrong.
I have a SQL query that's fetching a result like:
[{"id":89,"sender_id":2,"reciever_id":1,"message":"no reply","deleted_at":null,"created_at":"2017-04-04 17:01:20","updated_at":"2017-04-04 17:01:20"},
{"id":88,"sender_id":1,"reciever_id":2,"message":"hiiiii","deleted_at":null,"created_at":"2017-04-04 16:59:47","updated_at":"2017-04-04 16:59:47"},
{"id":87,"sender_id":5,"reciever_id":1,"message":"hiiiii","deleted_at":null,"created_at":"2017-04-04 16:59:43","updated_at":"2017-04-04 16:59:43"},
{"id":86,"sender_id":1,"reciever_id":5,"message":"hiiii","deleted_at":null,"created_at":"2017-04-04 16:59:38","updated_at":"2017-04-04 16:59:38"},
{"id":85,"sender_id":1,"reciever_id":5,"message":"hiii vRUN\n","deleted_at":null,"created_at":"2017-04-04 16:10:51","updated_at":"2017-04-04 16:10:51"}]
But I want only unique combinations of sender_id and reciever_id. I need only 2 rows out of 5 because only 2 rows have unique combination. Like this:
[{"id":89,"sender_id":2,"reciever_id":1,"message":"no reply","deleted_at":null,"created_at":"2017-04-04 17:01:20","updated_at":"2017-04-04 17:01:20"},
{"id":87,"sender_id":5,"reciever_id":1,"message":"hiiiii","deleted_at":null,"created_at":"2017-04-04 16:59:43","updated_at":"2017-04-04 16:59:43"},
How can I achieve that?
add a GROUP BY like this
SELECT *
FROM yourTable
GROUP BY SUBSTRING_INDEX(SUBSTRING_INDEX( ,',',3),':',-1)
sample
mysql> SELECT SUBSTRING_INDEX(SUBSTRING_INDEX('[{"id":89,"sender_id":2,"reciever_id":1,"message":"no reply","deleted_at":null,"created_at":"2017-04-04 17:01:20","updated_at":"2017-04-04 17:01:20"},' ,',',3),':',-1) as result;
+--------+
| result |
+--------+
| 1 |
+--------+
1 row in set (0,00 sec)
mysql>
Suppose I have a table named "t1" which contains a column named "ID" which has the following records
abcde=1=2
qwert=3
hhhhj=9
zxcv=5=8
How can I extract the records that contain only 1 "=" sign by using REGEXP in MySQL?
I've tried
SELECT * FROM t1 WHERE ID REGEXP '\\w*=\\w*=\\w*'; -- returns no records
SELECT * FROM t1 WHERE ID REGEXP '\\w*=\\w*'; -- returns all 4 records
I expected the first query to return the records which contains 2 "=", and the second query to return the records which contains only 1 "=".
What's wrong with my queries?
A pretty simple solution without REGEXP would be
select * from table
where
length(ID) - length(replace(ID,'=','')) = 1 ;
Some test cases
mysql> select length('qwert=3') - length(replace('qwert=3','=','')) as diff;
+------+
| diff |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
mysql> select length('zxcv=5=8') - length(replace('zxcv=5=8','=','')) as diff;
+------+
| diff |
+------+
| 2 |
+------+
1 row in set (0.00 sec)
SELECT * FROM t1 WHERE ID REGEXP '^[^=]*=[^=]*$';
Guess this should do it.See fiddle http://www.sqlfiddle.com/#!9/b2ead/2/0
While I was trying to solve This Question. I created the dummy records in a table
create table mytable(data CHAR(30));
INSERT INTO mytable VALUES('d\\one'),('d\\two'),('d\\three');
SELECT * FROM mytable;
+---------+
| data |
+---------+
| d\one |
| d\two |
| d\three |
+---------+
3 rows in set (0.00 sec)
Now when i am selecting records, I am getting no result, I have tried many combination with like but no luck.
Ex :
SELECT * FROM mytable WHERE data LIKE "d\\%";
Empty set (0.00 sec)
SELECT * FROM mytable WHERE data LIKE 'd\\%';
Empty set (0.00 sec)
Use triple slash:
SELECT * FROM mytable WHERE data LIKE "d\\\%"
Or use INSTR() instead
SELECT * FROM mytable WHERE instr(data, 'd\\') = 1
I want to get the maximum value of a column for the first 1000 not null results for some condition. Then, when for the next 1000, and so on. I do this for different conditions, but here I found something strange, when I use dayofweek. The first command I show you works:
mysql> select max(id),max(d20) from (select id, d20 from data where d20 is not null and id<1000000 and dayofweek(day)=1 limit 1000) x;
+---------+----------+
| max(id) | max(d20) |
+---------+----------+
| 100281 | 13785 |
+---------+----------+
1 row in set (0.44 sec)
but actually I want this second command, which doesn't work as expected.
mysql> select max(id),max(d20) from (select id, d20 from data where d20 is not null and id>100000 and dayofweek(day)=1 limit 1000) x;
+---------+----------+
| max(id) | max(d20) |
+---------+----------+
| 303765 | 0 |
+---------+----------+
1 row in set (0.02 sec)
Any clue?
Take the extreme case of the limit being 1.
That means, the subquery returns any row (there's no order by to make the row deterministic) that has id<1000000, which makes MAX(id) and MAX(d20) return the values from that row only. Hardly representative of the total collection.
Raising the limit to 1000 will just make the sample bigger, but will still give an indeterministic result depending on which 1000 rows are sampled (assuming there are more than 1000 rows that match). You may very well get a different result every time you execute the query, so expecting a particular result won't work.
If you need a deterministic result, add an ORDER BY to your subquery before limiting the results.