SQL check in two tables [duplicate] - mysql

This question already has answers here:
SELECT * WHERE NOT EXISTS
(5 answers)
Closed 8 years ago.
Hope you can help me.
I have 2 tables in my MqSQL database.
Table 1.
speedywebs_data
cardid
1
2
3
4
Table 2:
Speedywebs_results
resultid / card1 / card2
1 / 2 / 1
2 / 4 /4
my problem is, i want to get all posts in speedywebs_data table, but only cardid's whos not listed in the speedywebs_results cardid1. How can i do that?

SELECT speedywebs_data.*
FROM
speedywebs_data
WHERE
cardid NOT IN (SELECT card1 FROM Speedywebs_results WHERE card1 IS NOT NULL)
or you could use also this:
SELECT speedywebs_data.*
FROM
speedywebs_data LEFT JOIN Speedywebs_results
ON speedywebs_data.cardid = Speedywebs_results.card1
WHERE
Speedywebs_results.card1 IS NULL

You wanna check if a value doesn't exist ?
So use... NOT EXISTS.
select cardid
from speedywebs_data swd
where not exists (select null
from speedywebs_results swr
where swr.card1 = swd.cardid)

Related

Selecting columns from table after joining in SQL [duplicate]

This question already has answers here:
Should I Always Fully Qualify Column Names In SQL?
(11 answers)
JOIN - Is it a good practice to add table name before column name?
(3 answers)
Closed 6 months ago.
is there a difference between specifying the table-column after joining tables?
For example, in the below queries I selected the columns as b.refunded_at or refunded_at. The output the same, but I just want to make sure.
Thanks
SELECT billing_cycle_in_months,
MIN(DATEDIFF(refunded_at, settled_at)) AS min_days,
AVG(DATEDIFF(refunded_at, settled_at)) AS avg_days,
MAX(DATEDIFF(refunded_at, settled_at)) AS max_days
FROM noom_signups AS a
JOIN noom_transactions AS b ON a.signup_id = b.signup_id
JOIN noom_plans AS c ON a.plan_id = c.plan_id
WHERE started_at >= 2019-01-01
GROUP BY 1;
and
SELECT billing_cycle_in_months,
MIN(DATEDIFF(b.refunded_at, b.settled_at)) AS min_days,
AVG(DATEDIFF(b.refunded_at, b.settled_at)) AS avg_days,
MAX(DATEDIFF(b.refunded_at, b.settled_at)) AS max_days
FROM noom_signups AS a
JOIN noom_transactions AS b ON a.signup_id = b.signup_id
JOIN noom_plans AS c ON a.plan_id = c.plan_id
WHERE a.started_at >= 2019-01-01
GROUP BY 1;
Tables do not have to be specified so long as the column names are unambiguous.

How to merge the result of two select mysql query [duplicate]

This question already has answers here:
How can I get multiple counts with one SQL query?
(12 answers)
Closed 1 year ago.
my first query is
select GEAR,count(GEAR)
from new_failure
where STN_CODE = "BVH" group by(Gear);
and its result is
result if image is not visible
# GEAR Total
SIGNAL 8
POINT 16
HASSDAC 5
,SIGNAL 1
SSDAC 1
TRACK CIRCUIT 9
UFSBI 2
DC 1
2nd query
select GEAR,count(GEAR)
from new_failure
where STN_CODE = "BVH"
and MONTH(fail_time) = 4
group by(Gear);
result
# GEAR April
SIGNAL 3
POINT 4
HASSDAC 1
,SIGNAL 1
SSDAC 1
i want result in the form given in image below
You can use either LEFT JOIN, RIGHT JOIN or JOIN depending on what you are aiming to get,
SELECT *
FROM ( select GEAR,count(GEAR)
from new_failure
where STN_CODE = "BVH" group by(Gear) AS A
JOIN ( select GEAR,count(GEAR)
from new_failure
where STN_CODE = "BVH"
and MONTH(fail_time) = 4 AS B
ON A.orders_id=B.orders_id
or you can refer to this link for a similar question
joining two select statements

MySQL count how many times foreign key has a given value [duplicate]

This question already has answers here:
Selecting users who were not sent newsletter
(2 answers)
Closed 5 years ago.
I have one table with states and one table with dates under a given state
s_state
---------
#id name
1 State 1
2 State 2
d_date
--------
#date #user state
2017-01-01 1 1
2017-01-02 1 1
2017-01-03 2 1
I am trying to get, for a given user, how many times (how many days) he had been with each state. My current query works if the state is used, but my problem is that it doesn't return "count 0" for the states not used. (It would, for user 1, return only "State 1 used 2 times", but I want it to return "State 1 count = 2, State 2 count = 0")
Here is my current query:
SELECT s_state.id, COUNT(date)
FROM s_state
LEFT JOIN d_date ON s_state.id = d_date.state
WHERE d_date.user = 1
GROUP BY s_state.id
Try this
SELECT
s_state.id AS 'State Id',
IFNULL(COUNT(date), 0) AS 'Count'
FROM s_state
LEFT JOIN d_date ON s_state.id = d_date.state AND user = 1
GROUP BY
s_state.id,
user
If you use user in WHERE clause, it will filter those that do not exist. A JOIN will show NULLs instead, which you can then convert to 0s

Sort varchar column in mySql [duplicate]

This question already has answers here:
MySQL 'Order By' - sorting alphanumeric correctly
(20 answers)
Closed 7 years ago.
I am trying to sort one column having values
FMOL1001,
FMOL1004,
FMOL1009,
FMOL10010,
FMOL1003,
FMOL10025
But it is not sorting properly,please help
Try this, but im not sure...
If you made a numeric string like this FMOLXXXX-->>XXX then you can sort XXX:
SELECT column, SUBSTRING(column FROM 5) sort FROM table ORDER by CAST(sort AS UNSIGNED)
Sample data :
id name
-------------
1 FMOL1001
2 FMOL1004
3 FMOL1009
4 FMOL10010
5 FMOL1003
6 FMOL10025
Query :
SELECT id, name
FROM table
ORDER BY LPAD(SUBSTR(name,5,LENGTH(name)-4),10,'0') ASC
Output :
id name
-------------
1 FMOL1001
5 FMOL1003
2 FMOL1004
3 FMOL1009
4 FMOL10010
6 FMOL10025
Explanation :
LPAD(SUBSTR(name,5,LENGTH(name)-4),10,'0')
Will produce :
FMOL0000001001
FMOL0000001003
FMOL0000001004
FMOL0000001009
FMOL0000010010
FMOL0000010025

Multiple aggregate function for mysql. like SUM [duplicate]

This question already has answers here:
Get rows product (multiplication)
(3 answers)
Closed 9 years ago.
How can I multiple numbers from table to one result like SUM() does?
In "table1" I have column "number1" with these values:
table1.number1
--------------
1
2
3
I try this sql:
SELECT #multiple := #multiple number1 as mul
FROM table1
and I got this:
mul
---
1
2
6
But I need just the last row with the value: 6
without using ORDER DESC
Try this:
SELECT MAX(#multiple := #multiple * number1) AS mul
FROM table1, (SELECT #multiple:=1) a;