Mysql select rows from multiple tables in specific order - mysql

I have 2 tables in a mysql database ("comments" and "replies")
They have the schemas:
comments: id,userId,text
replies: id,userId,commentId,text
I need a mysql query that will fetch all comments from the comment table, and after each comment, the corresponding replies from the replies table...
so if we had: [comment 1] and [comment 2] in the comments table, and [reply 1] (to comment 1) and [reply 2] (to comment2) in the replies table
then it would return:
[comment 1]
[reply 1]
[comment 2]
[reply 2]

You would need to join the two tables & then order based on the commentID & replyID for multiple replies.
In the following I have added a dummy replyID of 0 for the original comment. The tables are joined using UNION ALL. Note that I have renamed the original id column of the comments table & reply id so they do not clash.
SELECT id commentID, userID, text, 0 replyID FROM test.comments
UNION ALL
SELECT commentID, userID, text, id replyID FROM test.replies
ORDER BY commentID, replyID;

Related

SQL: Return immediately if one matched record found

I have one table with user and their posts. It looks like "user_id | post_id | post_status".
Now I have a list of userid (ex, 100 users) and I want to know how many of them has at least one post that gets deleted (ex, post_status 3).
Here is my sample search:
select count(distinct user_id)
from post_table
where user_id in ( {my set} )
and post_status=3
It runs super slow since it iterates the entire table. Is there a way to speed up the query?
Use something like
SELECT COUNT(*)
FROM
-- the list of userid as a rowset
( SELECT 123 AS user_id UNION ALL
SELECT 456 UNION ALL
-- ...
SELECT 789
) user_id_list
WHERE EXISTS ( SELECT NULL
FROM post_table
WHERE post_table.user_id = user_id_list.user_id
AND post_table.post_status = 3 )
If your MySQL version is 8.0.4 or above then you may provide the users list as CSV/JSON and parse it using JSON_TABLE (the query text will be more compact).
INDEX(post_status, user_id)
may help speed up your query, especially if very few rows have status=3.
This could also speed up Akina's solution.

How can i write this SQL query in CYPHER?

This the following SQL Query
select count(distinct Descendent_id) from
(
select *
from v.ABC a
left join v.XYZ b
on a.Target_id=b.Ancestor_id
) t
where t.Target_id = 1234;
Edit
There's actually no relationship between the two tables / nodes. I'll explain -
Let's assume Table B has Ancestor_Id and Descendent_id which has some relationship between them we can call it [children]
Ancestor_Id - 101 --[children]--> Descendent_Id - 101.1
Ancestor_Id - 101 --[children]--> Descendent_Id - 101.2
Table A has only Target_Id which are the same as of Table B,
Eg - Table A - Target_Id = 101 | Table - B Ancestor_Id = 101
Note -
Table A doesn't have any relationship with Table B.
So I need to join the two tables / nodes with Target_Id and Ancestor_ID and what ultimately i want is DISTINCT COUNT of Descendent_Id from Table B which has a [children] relationship with Ancestor_Id.
Also from the SQL Query if you can see Descendent_id doesn't has any
alias pre-pended to it like b.Descendent, how do I achieve the same in
Neo4j.
Kindly let me know if I've still not cleared the doubt properly.
I am very new in CYPHER - Neo4j, KINDLY HELP!
[UPDATED]
The following Cypher will get the number of distinct D_id values in the "left join" of abc and xyz nodes having the specified ID.
MATCH (a:abc) WHERE a.id = $id
OPTIONAL MATCH (b:xyz) WHERE b.abc_id = $id
WITH COLLECT(a.D_id)+COLLECT(b.D_id) AS ids
UNWIND ids AS id
RETURN COUNT(DISTINCT id) AS D_id_count
I assume that abc nodes store the ID in an id property, and xyz nodes store it in an abc_id property. I also assume that the desired ID value is passed as an id parameter.
Note: this query would be faster if you created indexes on:
:abc(id)
:xyz(abc_id)

How could I get a list of unique records from below duplicated records by ignoring first two characters?

Here is the rule:
When comparing userId, only search userId starting with 'AB' and its matching duplicates (except 'AB'). Then get a list of "unique userId" by only returning above duplicated userId that is having 'AB' at the beginning.
For returned duplicated string starting with 'AB', we need to make sure there is "duplicate"; otherwise, we should not return 0 record
I know it sounds confusing, please see example below:
Table UserName with ten records, and its userId fields (10 records) are:
ABC1234
C1234
C12345
BC12345
BBC1234
ABF1235
F1235
ABY1236
BCD3456
D3456
Desired Result after running query:
ABC1234
ABF1235
Please note: Although ABY1236 starts with 'AB', this record should not be returned in output, since it doesn't have a "duplicate" match like Y1236 (ignoring first two character, 'AB').
I have a sample query below, but it only returned duplicated record NOT starting with 'AB', also it will return ABY1236.
SELECT distinct substr(userId , -(length(userID)-2))
from UserName where userId like 'AB%';
Thanks for the help!
You can use EXISTS to check if there is a userId that is equal to the right part of "AB.." starting from the 3d char:
select u.userId from UserName u
where
u.userId like 'AB_%'
and
exists (
select 1 from UserName where userId = substr(u.userId, 3)
)
You could try using a selct join for check only the matching result
SELECT substr(a.userId , -(length(a.userID)-2))
from UserName a
INNER JOIN UserName b ON a.substr(a.userId , -(length(a.userID)-2)) = b.UserId
AND userId like 'AB%'

MySQL INSERT INTO table 1 SELECT table 2 with different column name

I have a table (pdt_1) in database (db_1) and another table (pdt_2) in another database (db_2).
I met pdt_1 and pdt_2 to find pdt_1 products not present and published in pdt_2.
functional code :
SELECT * FROM db_1.pdt_1 AS lm
WHERE lm.product_sku
NOT IN (SELECT DISTINCT product_cip7 FROM db_2.pdt_2)
AND lm.product_publish=‘Y'
finally, I need to insert the result of this query in pdt_2.
However, the structure of pdt_1 and pdt_2 are different.
Example:
- columns's names
- columns's numbers
I also need an auto_increment id for pdt_1 products inserted into pdt_2.
I need help.
NB : sorry for my poor english :(
If you want a new table with just the id and product_sku, try:
INSERT INTO new_table # with id and product_sku from first table
SELECT pdt_1.id,
pdt_1.product_sku
FROM db_1.pdt_1
LEFT JOIN db_2.pdt_2
ON pdt_1.product_sku = pdt_2.product_cip7
WHERE pdt_2.product_cip7 IS NULL
AND pdt_1.product_publish = 'Y'

mysql merge 2 tables,same structure, overlapping values in keys (auto_increment)

I have two tables, with the same structure, order_old and order_new. Key field on both is order_id which is auto_increment. order_old has 590 records in it, order_new has 100; I want to copy/add all the data from order_new into order_old, starting after the last record, but am not sure how to do this as the ordr_id keys in order_new are lower than those in order_old
any advise would be appreciated!
David
If no other tables are related to the two, you can use:
INSERT INTO order_old
( field2 --- do not include the order_id
, field3
, ...
)
SELECT
field2 --- same here
, fields3
, ...
FROM order_new
For other options, check this similar question: how-can-i-merge-two-mysql-tables