Mysql concat unequal table field - php

why I can't Select a row of a table with an unequal concat construction? I'll show you an example.
Table1
| id | area |
| 1 | items_labeling_small |
| 2 | items_labeling_big |
Table2
| id | area | kat |
| 1 | small | labeling |
| 2 | big | labeling |
SELECT Table1.area FROM Table1, Table2 WHERE Table1.area != CONCAT('items_', Table2.kat, '_', Table2.area)
No results have to be shown, because both are matching with the concat construction. But they're shown in result. I've no idea why.. and how I can change the query that it works.

They are show because each row don't match the other so you have the rows that not match
SELECT Table1.area
FROM Table1
INNER JOIN Table2 ON Table1.area != CONCAT('items_', Table2.kat, '_', Table2.area)
could be you want a not in
SELECT Table1.area
FROM Table1
where Table1.area NOT IN (
SELECT CONCAT('items_', Table2.kat, '_', Table2.area)
FROM Table2
)
and as a suggestion you should not use the (old) implict join sintax based on where clause ...use explicit join sintax ..

Related

PDO Query : Count related and repeated values then inner join

I work with PHP and PDO.
So I have 2 tables like,
Table 1
| id | name | age |
| 1 | John | 25 |
| 2 | Tom | 32 |
| 3 | James| 45 |
Table 2
| id | Comment | Link |
| 1 | some text | 3 |
| 2 | some text | 3 |
| 3 | some text | 1 |
So, Link column numbers represent id's in table1. For example Link = 3s in table 2 represent James in table 1. I need a query which brings all table1's data and also a number of repeated value for related Link column which comes from table2.
For example, the query should give me (let's choose James),
| id | name | age | Value |
| 3 | James | 45 | 2 |
value=2, because there are two 3s in link column which related to James
I tried somethings but got lots of errors.
I think you just need the GROUP BY
SELECT a.id,
a.name,
a.age,
count(*) as value
FROM table1 a
JOIN table2 b ON a.id = b.link
GROUP BY a.id, a.name, a.age
If you really want just one row then add WHERE
SELECT a.id,
a.name,
a.age,
count(*) as value
FROM table1 a
JOIN table2 b ON a.id = b.link
WHERE a.name = 'James'
GROUP BY a.id, a.name, a.age
or use subquery
SELECT a.id,
a.name,
a.age,
(SELECT count(*) FROM table2 b WHERE a.id = b.link) as value
FROM table1 a
WHERE a.name = 'James'

Count common rows between two tables in mysql

so here's my question...
Hi have two tables in mysql, called go_H and go_J, both looking like this:
go_H
+---------------+------------+
| gene | GoCode |
+---------------+------------+
| DNAJC25-GNG10 | GO:0004871 |
| DNAJC25-GNG10 | GO:0005834 |
| DNAJC25-GNG10 | GO:0007186 |
| LOC100509620 | GO:0005215 |
| LOC100509620 | GO:0006810 |
| LOC100509620 | GO:0016021 |
| PPIAL4E | GO:0000413 |
| PPIAL4E | GO:0003755 |
| PPIAL4E | GO:0005737 |
| PPIAL4E | GO:0006457 |
| LOC105371242 | GO:0000413 |
+----------------------------+
go_J
+------------+
| GoCode |
+------------+
| GO:0007254 |
| GO:0007256 |
| GO:0007257 |
| GO:0042655 |
| GO:0043506 |
| GO:0043507 |
| GO:0043508 |
| GO:0046328 |
| GO:0046329 |
| GO:0046330 |
+------------+
Basically what I want to achieve is to see what GoCode values from go_J appear in GoCode from Go_H, and count them, so as I get a total number o GO ids that are present in both tables.
I have come to select go_H.GoCode and go_J.GoCode, but I don't know how to compare them to find common rows and then count them...
Any help?
SELECT COUNT(*) FROM go_H
INNER JOIN go_J USING GoCode
INNER JOIN => Rows that are in both tables based on the join column (GoCode)
Alternative:
SELECT COUNT(*) FROM go_H h
INNER JOIN go_J ON j.GoCode = h.GoCode
Check this answer out to learn about joins:
What's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN?
Hope this helps.
select count(*) from go_J j join go_H h on h.GoCode=j.GoCode;
To find how many rows are similar between 2 table
SELECT COUNT(*) totalCount
FROM go_H a
INNER JOIN go_J b
ON a.GoCode = b.GoCode
To find how many rows from go_H are not in go_J
SELECT COUNT(*) totalCount
FROM go_H a
LEFT JOIN go_J b
ON a.GoCode = b.GoCode
WHERE b.GoCode IS NULL
To find how many rows from go_J are not in go_H
SELECT COUNT(*) totalCount
FROM go_J a
LEFT JOIN go_H b
ON a.GoCode = b.GoCode
WHERE b.GoCode IS NULL
You can achieve this just in SQL by running a query similar to this:
SELECT
*,
count (GoCode)
FROM (
SELECT GoCode FROM go_H
UNION
SELECT GoCode FROM go_H )a
group by a.gocode
This will provide you a table with each code in a column and then the amount of times it is present across both tables
An alternative with PHP would be get both tables into an array by using PDO and use in_array to check
<?php
foreach ($go_H as $GoCode) {
if (in_array($GoCode, $go_J)) {
// handle codes in both tables
}
}
This is not the most efficient method but it will yeild results.

SQL SELECT only rows having MAX value of a column from two different tables

My two table setup is like below:
table1
+------+---------+--------------------------------------+
| id | tail | content |
+------+---------+--------------------------------------+
| 1 | abc | ... |
| 2 | def | ... |
| 3 | ghi | ... |
| 4 | def | ... |
| 5 | jkl | ... |
+------+-------+----------------------------------------+
table2
+------+--------+---------------------------------------+
| id | tailID | value | others |
+------+--------+---------------------------------------+
| 1 | 2 | 412 | |
| 2 | 3 | 215 | |
| 1 | 2 | 571 | |
| 1 | 4 | 123 | |
+------+--------+---------------------------------------+
I like to get all columns from this two tables in a row with matched tail = tailID but not duplicate rows which has same tail.
For the duplicate TAIL, just need to get the single row of max VALUE of same tail.
I am currently using
SELECT table1.tail, table2.other_column
FROM table1
INNER JOIN table2
on table1.id = table2.tailID
WHERE table1.some_coloum = "a sepecific string"
ORDER BY table2.value
But it returns many duplicates of same tail.
I just need to have single row for duplicate TAIL with hightes VALUE of table2.
DISTINCT with CROSS APPLY:
SELECT DISTINCT t1.tail,
t2.other_column,
t3.[value]
FROM table1 t1
CROSS APPLY (
SELECT tailid,
MAX([value]) as [value]
FROM table2
WHERE tailid = t1.id
GROUP BY tailid
) as t3
INNER JOIN table2 t2
ON t2.tailid = t3.tailid AND t3.[value] = t2.[value]
WHERE t1.some_coloum = "a sepecific string"
First group table2 then join
SELECT table1.tail, table2.other_column
FROM table1
INNER JOIN (
SELECT tailID, max(value) as value
FROM table2
GROUP BY tailID
) t2g ON t2g.tailID = table1.ID
INNER JOIN table2
on t2g.tailID = table2.tailID AND t2g.value = table2.value
WHERE table1.some_coloum = "a sepecific string"
ORDER BY table2.value
The query still may return multiple rows for a table1 row if there are 2 or more rows in table2 with the same max(value) and tailID.
Selected only rows where is MAX value of column value
SELECT table1.tail, MAX(table2.value)
FROM
table1
INNER JOIN table2 ON table1.id = table2.tailID
WHERE table1.content = "test"
http://sqlfiddle.com/#!9/b70d29/3/0

MYSQL statement: lookup how many records exist how many times in other table

I have 2 database tables:
Table 1:
+---------+-------+-------------+
| Page | Title | Description |
+---------+-------+-------------+
| Apple | ..... | ........... |
| Orange | ..... | ........... |
| Pear | ..... | ........... |
| Grapes | ..... | ........... |
+---------+-------+-------------+
Table 2:
+----------+-------------+
| Link | Page |
+----------+-------------+
| Website1 | Apple |
| Website2 | Orange |
| Website3 | Apple |
| Website4 | Orange |
| Website5 | Apple |
| Website6 | Pear |
| Website7 | Apple |
| Website8 | Grapes |
| Website9 | Grapes |
+----------+-------------+
I want to know/return how many pages from Table 1 are referenced in Table 2 and how many times they are referenced. (I DON'T want to know how many times EACH page in Table 1 is referenced in Table 2).
So in this example:
1 page is referenced 1 time (Pear),
2 pages are referenced 2 times (Grapes and Orange) &
1 page is referenced 4 times.
What kind of SQL statement would I use to get this?
Following query should do..
SELECT COUNT(1) NoOfPages,CNT ReferencedTimes
FROM
(
SELECT T2.PAGE,COUNT(1) CNT
FROM TABLE1 T1 INNER JOIN TABLE2 T2 ON T1.PAGE = T2.PAGE
GROUP BY T2.PAGE
)T
GROUP BY CNT
I think the following statement will fit:
SELECT count(*) FROM Table2 WHERE (Table2.Page IN (SELECT Page FROM Table1));
Use This query
Select table2.page,cnt(table2.page)
from table1 inner join table2
On table1.Page=table2.Page group by table2.page
SELECT (GROUP_CONCAT(DISTINCT page)) AS Page,page_count
FROM
(SELECT table1.Page as page,COUNT(*) as page_count
FROM table1 INNER JOIN table2 ON table1.Page=table2.Page
GROUP BY table1.Page)
as T GROUP BY page_count
Hope this helps
If what you are seeking is X page was referenced N times, the below query will achieve that:
SELECT COUNT(t1.page), t2.count
FROM table1 t1
INNER JOIN (SELECT page,COUNT(*) AS count FROM table2 GROUP BY page) t2 ON t1.page=t2.page
GROUP BY t2.count
Try this query, it will make a left join and tell you how many times item is referenced in table2, if count is zero than no reference in the other table
SELECT table1.Page, count(table2.Page) as count
FROM table1
LEFT JOIN table2 ON table2.Page = table1.Page
GROUP BY table1.Page

Insert Comma Separated values through MySQL query

I have one table (in phpmyadmin) with the following fields and structure:
Table 1
id | sponsor
1 | -1
2 | 1
3 | 1
4 | 2
5 | 4
6 | 4
Now i want to insert data from above table into new table as below:
Table 2
id | children
1 | 2,3
2 | 4
3 |
4 | 5,6
5 |
6 |
Actually this is Tree structure, which i have saved in mysql database.
I have already written a script in php but as there are more then 100K rows in table 1 so its taking too much time. Please tell me an efficient sql query to do this task quickly.
Query:
SQLFIDDLE Example
SELECT
t1.id,
(SELECT group_concat(id separator ', ')
FROM table1 t2
WHERE t2.sponsor = t1.id) AS children
FROM table1 t1
GROUP BY t1.id
Result:
| ID | CHILDREN |
-----------------
| 1 | 2, 3 |
| 2 | 4 |
| 3 | (null) |
| 4 | 5, 6 |
| 5 | (null) |
| 6 | (null) |
Insert Statement:
INSERT INTO table2
SELECT
t1.id,
(SELECT group_concat(id separator ', ')
FROM table1 t2
WHERE t2.sponsor = t1.id) AS children
FROM table1 t1
GROUP BY t1.id
This is similar to #Justin's answer but uses a left join instead of a correlated subquery:
INSERT INTO Table2 (id, children)
SELECT
sp.id,
GROUP_CONCAT(ch.id) AS children
FROM Table1 sp
LEFT JOIN Table1 ch ON sp.id = ch.sponsor
GROUP BY t1.id
;
A demonstration of the SELECT statement's result can be found (and played with) at SQL Fiddle (the schema having been borrowed from Justin).
One of your SELECT elements should be a GROUP_CONCAT(...) as column, which will concatenate those values separated with commas. If you want to filter by one of those values, you can use GROUP BY -whatever- HAVING find_in_set( -number- , column )
See if the following helps
INSERT INTO table2
SELECT sponsor, GROUP_CONCAT(id)
FROM table1
GROUP BY id

Categories