Cartesian Product Single Table - mysql

I have a very simple MySQL table with three attributs, let's call it word_tbl
id | word | count
I want to combine each row with all rows in the table and sum the value in count.
The results should be saved in a new table, called cartesian_tbl.
In my opinion, the cartesian_tbl should look like this:
id | word1_id | word2_id | count
My Problem of understanding are the two word_ids which are both a Foreign Key of word_id.id.
Is my schema correct or is there a simpler solution to reach my target?
Thank you for your help!
New:
These 2 tables are only an example...
Here a little example how the combination should work
If "Hello" with id 2 has count 4 and "Example" with id 32 has count 5. The new Datarow should look like this
1, 2,32,9
#Francois T
count is only a number
What additional informations do you need?

INSERT INTO cartesian_tbl (word1_id, word2_id, count)
SELECT w1.id, w2.id, w1.count+w2.count
FROM word_tbl w1
JOIN word_tbl w2

Create a table with id auto_increment
And something like that :
but what do you want into count ?
insert into cartesian_tbl (world1_id,world2_id,count)
select word1.id, word2.id, count??? from word_tbl word1 , word_tbl word

Related

How to get exact rows count of particular column in MySQL table

I want to get exact row count of specified column
Example: Table
Name Id Age
_______________________________
Jon 1 30
Merry 2 40
William 50
David
There are 4 rows in table but i want to count ID column.
I am using below query to achieve it
select count(Id) from table;
But its returning 4 and I know why it is returning 4 but I want output as 2 because there are only two rows in Id column.
How can i achieve it?
Try this:
select count(Id) from table where id>0;
with the help of #blabla_bingo and #Edwin Dijk finally i have achieved it by below query
select count(Id) from table where Id!="";

Histogram using to data tables (SQL query)

I want to make a histogram of the number of comments per user in January 2019 (including the once that haven't commented)
The tables I'm working with look like this:
id
Name
1
Jose
2
Pedro
3
Juan
4
Sofia
user_id
Comment
Date
1
Hello
2018-10-02 11:00:03
3
Didn't Like it
2018-06-02 11:00:03
1
Not so bad
2018-10-22 11:00:03
2
Trash
2018-7-21 11:00:03
I think I'm overcomplicating it. But here is my try:
#Here I'm counting how much comments are per person that have commented.
CREATE TABLE aux AS
SELECT user_id, COUNT(user_id)
FROM Undostres
GROUP BY user_id;
#With the following code, I end up with a table with the missing values (ids that haven't commented)
CREATE TABLE Test AS
SELECT DISTINCT user_id +1
FROM aux
WHERE user_id + 1 NOT IN (SELECT DISTINCT user_id FROM aux);
ALTER TABLE Test RENAME COLUMN user_id +1 TO ;
INSERT INTO Undostres (user_id)
SELECT user_id FROM Test;
It returns an error when I try to rename user_id+1 with other name. So I can't keep going.
Any suggestions would be great!
I would do it this way:
CREATE TABLE aux AS
SELECT Users.user_id, COUNT(Undostres.user_id) AS count
FROM Users
LEFT OUTER JOIN Undostres USING (user_id)
GROUP BY Users.user_id;
I am assuming you have a table Users that enumerates all your users, whether they have made any comments or not. The LEFT OUTER JOIN helps in this case, because if there are no comments for a given user, the user is still part of the result, and the COUNT is 0.

SQL Validate a column with the same column

I have the following situation. I have a table with all info of article. I will like to compare the same column with it self. because I have multiple type of article. Single product and Master product. the only way that I have to differences it, is by SKU. for example.
ID | SKU
1 | 11111
2 | 11112
3 | 11113
4 | 11113-5
5 | 11113-8
6 | 11114
7 | 11115
8 | 11115-1-W
9 | 11115-2
10 | 11116
I only want to list or / and count only the sku that are full unique. follow th example the sku that are unique and no have variant are (ID = 1, 2, 6 and 10) I will want to create a query where if 11113 are again on the column not cout it. so in total I will be 4 unique sku and not "6 (on total)". Please let me know. if this are possible.
Assuming the length of master SKUs are 5 characters, try this:
select a.*
from mytable a
left join mytable b on b.sku like concat(a.sku, '%')
where length(a.sku) = 5
and b.sku is null
This query joins master SKUs to child ones, but filters out successful joins - leaving only solitary master SKUs.
You can do this by grouping and counting the unique rows.
First, we will need to take your table and add a new column, MasterSKU. This will be the first five characters of the SKU column. Once we have the MasterSKU, we can then GROUP BY it. This will bundle together all of the rows having the same MasterSKU. Once we are grouping we get access to aggregate functions like COUNT(). We will use that function to count the number of rows for each MasterSKU. Then, we will filter out any rows that have a COUNT() over 1. That will leave you with only the unique rows remaining.
Take that unique list and LEFT JOIN it back into your original table to grab the IDs.
SELECT ID, A.MasterSKU
FROM (
SELECT
MasterSKU = SUBSTRING(SKU,1,5),
MasterSKUCount = COUNT(*)
FROM MyTable
GROUP BY SUBSTRING(SKU,1,5)
HAVING COUNT(*) = 1
) AS A
LEFT JOIN (
SELECT
ID,
MasterSKU = SUBSTRING(SKU,1,5)
FROM MyTable
) AS B
ON A.MasterSKU = B.MasterSKU
Now one thing I noticed from you example. The original SKU column really looks like three columns in one. We have multiple values being joined with hypens.
11115-1-W
There may be a reason for it, but most likely this violates first normal form and will make the database hard to query. It's part of the reason why such a complicated query is needed. If the SKU column really represents multiple things then we may want to consider breaking it out into MasterSKU, Version, and Color or whatever each hyphen represents.

Find a string in one column if exist than give that value othere wise find in other column in a single mysql query

I have 3 column in database with 20000 records.
Let take a example
id | country_string | country_code<br>
275| Bangalore,BLR-India | BLR<br>
375| Basongo,BAN | BAN
I have given one record. I'm searching BLR. First it should match from country_code column if there then it will return the record otherwise it should search in country_string.It should be in single query.
if I'm using LIKE query And I search BAN It will give both record but It should come record number 375 and searching bang it should give record number 275
You can do this with LIKE.
Query
SELECT * FROM tableName
WHERE country_code LIKE '%BAN%'
OR country_string LIKE '%,BAN%';
select * from table where
country_code like '%$search_name%'
OR country_string like '%$search_name%'

find out count of comma based value in MySql

I have two tables.
Table Emp
id name
1 Ajay
2 Amol
3 Sanjay
4 Vijay
Table Sports
Sport_name Played by
Cricket ^2^,^3^,^4^
Football ^1^,^3^
Vollyball ^4^,^1^
Now I want to write a query which will give me output like
name No_of_sports_played
Ajay 2
Amol 1
Sanjay 2
Vijay 2
So what will be Mysql query for this?
I agree with the above answers/comments that you are not using a database for what a database is for, but here is how you could calculate your table from your current structure in case you have no control over that:
SELECT Emp.name, IF(Played_by IS NULL,0,COUNT(*)) as Num_Sports
FROM Emp
LEFT JOIN Sports
ON Sports.Played_by RLIKE CONCAT('[[:<:]]',Emp.id,'[[:>:]]')
GROUP BY Emp.name;
See it in action here.
UPDATE: added the IF(Played_by IS NULL,0,COUNT(*)) instead of COUNT(*). This means that if an employee doesn't play anything they'll have a 0 as their Num_Sports. See it here (I also added in those ^ characters and it still works.
What it does is joins the Emp table to the Sports table if it can find the Emp.id in the corresponding Played_by column.
For example, if we wanted to see what sports Ajay played (id=1), we could do:
SELECT *
FROM Emp, Sports
WHERE Sports.Played_by LIKE '%1%'
AND Emp.id=1;
The query I gave as my solution is basically the query above, with a GROUP BY Emp.name to perform it for each employee.
The one modification is the use of RLIKE instead of LIKE.
I use RLIKE '[[:<:]]employeeid[[:>:]]' instead of LIKE '%employeeid%. The [[:<:]] symbols just mean "make sure the employeeid you match is a whole word".
This prevents (e.g.) Emp.id 1 matching the 1 in the Played_by of 3,4,11,2.
You do not want to store your relationships in a column like that. Create this table:
CREATE TABLE player_sports (player_id INTEGER NOT NULL, sport_id INTEGER NOT NULL, PRIMARY KEY(player_id, sport_id));
This assumes you have an id column in your sports table. So now a player will have one record in player_sports for each sport they play.
Your final query will be:
SELECT p.name, COUNT(ps.player_id)
FROM players p, player_sports ps
WHERE ps.player_id = p.id
GROUP BY p.name;