SQL QUERY----Retrieve all email - php

$q="SELECT DISTINCT
t1.un_email,t6.email
FROM
sisters_7sisters.tbl_unsubscribe as t1,
sisters_7sisters.tbl_uptade_list as t6";
I write a query like above I need to retrieve all the unique emails from the tables
But it gives me pair of some results which are not unique ..I tried to get the email only once
from these tables.

It would be better to use a join to join those two tables and select the distinct email
The distinct you are using, is only applied to each column of the result and not both at the same time.

It you are not joing them and you do not have any where clause. Then you might be fine with this solution (No duplicate emails):
SELECT
t1.un_email AS email
FROM
sisters_7sisters.tbl_unsubscribe as t1
UNION
SELECT
t6.email
FROM
sisters_7sisters.tbl_uptade_list as t6
Or you can use a UNION ALL. This will return the duplicates:
SELECT
t1.un_email AS email
FROM
sisters_7sisters.tbl_unsubscribe as t1
UNION
SELECT
t6.email
FROM
sisters_7sisters.tbl_uptade_list as t6

Ithink you have forgot Join ocndition
when you use
select * from table1,table2
it will create a cartisian product
if table1 as
1
2
3
and table2 has
4
5
6
as their data.
cartesian product will be
1 4
1 5
1 6
2 4
2 5
2 6
3 4
3 5
3 6
to remove this unnecesary fields
you have to Join

Related

Simple way to calculate average by union of three table in MySQL

I have a union of three tables (t1,t2,t3). Each return exactly the same number of records, first column is id, second amount:
1 10
2 20
3 20
1 30
2 30
3 10
1 20
2 40
3 60
Is there a simple in SQL way to calculate the average up to only get:
1 20
2 30
3 30
can the new value store in an another table t4 with php code?
One way to get an average is to use the SQL AVG() aggregate function.
If we want an average "per id", then we need to include a GROUP BY clause.
We can use an inline view query in place of a table reference. (MySQL refers to this as a derived table.
Here's an example of what the query might look like:
SELECT t.id
, AVG(t.amount) AS avg_amount
FROM ( SELECT t1.id
, t1.amount
FROM t1
UNION ALL
SELECT t2.id
, t2.amount
FROM t2
UNION ALL
SELECT t3.id
, t3.amount
FROM t3
) t
GROUP BY t.id
ORDER BY t.id

Improve performance of SQL Query to select ids from different tables in multiple columns

Let us just imagine I have the following table structure
Table: images
id path
1 x
2 x
3 x
4 x
5 x
6 x
7 x
8 x
Table: user
id image imageSmall
1 1 1
2 2 2
3 4 4
Table: books
id image imageSmall
1 5 5
2 6 6
3 8 8
I now want to get the ID of every image used in other tables. I made this query here
SELECT id FROM images WHERE id IN (SELECT image FROM user) OR id IN (SELECT imageSmall FROM user) OR id IN (SELECT image FROM books) OR id IN (SELECT imageSmall FROM books);
The problem I see here, is that, when I have a large amount of data, this query could be very time consuming and not performant at all because of the many IN parts of the query. Is there a way to improve the performance of this query?
I would phrase this using exists rather than in:
SELECT id
FROM images i
WHERE EXISTS (SELECT 1 FROM user u WHERE u.image = i.id) OR
EXiSTS (SELECT 1 FROM user u WHERE u.imageSmall = i.id) OR
EXISTS (SELECT 1 FROM books b WHERE b.image = i.id);
For performance, be sure that you have the following indexes:
create index idx_user_image on user(image);
create index idx_user_imageSmall on user(imageSmall);
create index idx_books_image on books(image);
Use joins instead of that many select in selects, and DISTINCT to return unique values:
SELECT DISTINCT(i.id) FROM images i
INNER JOIN `user` u
INNER JOIN `books` b
WHERE b.image=i.id OR b.imageSmall=i.id OR u.image=i.id OR u.imageSmall=i.id

How to count same string in an array

I have a little problem , I want to count same string in an array ,
for example
My table like this:
id | data
---------------------------
1 | #user1,#user2,#user3
2 | #user1,#user4
3 | #user1,#user5
4 | #user2,#user3
How can I count #user1,#user2,etc.. ?
You can use find_in_set to find data in comma separated field.
SELECT COUNT(*)
FROM some_table
WHERE FIND_IN_SET('#user2', data)
This will give you a count of the rows that contain this string.
Note that this does suggest a database design that is not normalised and as this function can't use indexes it is likely to perform badly compared to a properly normalised database (ie, split the strings off onto a different table, with one row per string per id).
EDIT - if you want a count of all the strings:-
SELECT sub1.aString, COUNT(*)
FROM
(
SELECT DISTINCT SUBSTRING_INDEX(SUBSTRING_INDEX(data, ',', 1 + units.i + 10 * tens.i), ',', -1) AS aString
FROM some_table,
(SELECT 0 AS i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) units,
(SELECT 0 AS i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) tens
) sub1
INNER JOIN some_table
ON FIND_IN_SET(sub1.aString, data)
GROUP BY sub1.aString
This uses a couple of sub queries to generate 100 rows for each row in you main table, each with a combination of the numbers 0 to 9 twice. From the combination it can calculate a number between 0 and 99 (can easily be expanded to add another sub query to go from 0 to 999, or more). It then uses SUBSTRING_INDEX with the generated number to split out the possible strings in data for each row. This will generate a LOT of duplicates, partly as the strings will likely be on many rows and partly because the last string on each row will be put out many times (ie, if there are 10 string, the last one will be put out 91 times due to the way SUBSTRING_INDEX is used). DISTINCT is used to remove these duplicates.
The result is then joined against your table using FIND_IN_SET, and COUNT / GROUP BY used to get all the counts of all the strings.
You can try somthing like this:-
SELECT COUNT(data)
FROM your_table
WHERE data LIKE '%#user1%'

create a result set difference between two tables in Mysql

I have two table suppose products and auto_assign_prod_list. I want to populate a dropdown list with the id of products table that are not present in auto_assign_prod_list table.
Suppose,
product table contain
Id
------
1
2
3
4
5
auto_assign_prod_list table contain
Id
-----
1
5
So, my result set will be
2
3
4
How is it possible using MySQL and PHP ?
Try this:
SELECT Id FROM product
WHERE Id NOT IN (SELECT Id FROM auto_assign_prod_list)
It will select the ids from product table which are not in auto_assign_prod_list table.
Result:
Id
------
2
3
4
See result in SQL Fiddle.
use a left join
select p.id
from products p
left join auto_assign_prod_list a on a.id = p.id
where a.id is null
SQLFiddle demo
See this great explanation of joins

Compare and insert from multiple tables?

There are 4 tables table1,table2,table3 and table4
table1 has got 60000 datas
table2 has got 85000 datas
table3 has got 78000 datas
table4 has got 68000 datas
indexes on all tables are same but one got less than or more than each other. for example name john is stored in all 4 tables.but mathew is stored in may be two tables but not in other two and ethan may be stored in 3 tables but not may be in 4th one.
upto say first 60k all index/names are same but after that it is irregular
so how can I merge all these table into 1 table?? all four tables got 2 columns each and first one is name and second is its details
There are probably more efficient ways of doing this, but this was the first thing that came to mind.
INSERT INTO table5
SELECT DISTINCT Table5Content.* FROM (
SELECT * FROM table1
UNION ALL
SELECT * FROM table2
UNION ALL
SELECT * FROM table3
UNION ALL
SELECT * FROM table4
UNION ALL
SELECT * FROM table5
) as Table5Content

Categories