php sql merge 2 queries - sql

I have 2 queries one straight after the other:
$sql_result8 = mysql_query("SELECT * FROM properties WHERE c4='$id'", $db); $charges = mysql_num_rows($sql_result8);
$sql_result8 = mysql_query("SELECT * FROM vehicles WHERE c4='$id'", $db); $charges = $charges + mysql_num_rows($sql_result8);
What kind of query would I need to merge these? Some kind of JOIN? UNION?

SELECT * FROM properties p, vehicles v WHERE p.c4 = v.c4 AND p.c4 = '$id'

Try this
SELECT * FROM properties JOIN vehicles USING (c4) WHERE c4='$id'

If you want to just find out the number or rows returned from both queries and not show the actual columns, you can use this:
$sql_result8 = mysql_query(
"SELECT
( SELECT COUNT(*) FROM properties WHERE c4='$id' )
+ ( SELECT COUNT(*) FROM vehicles WHERE c4='$id' )
", $db );

Related

Subqueries in Python

I am trying to use subqueries to run matching against multiple tables and move the unmatched records to new table.
I have written SQL subqueries but the only issue i am facing is the performace, it is taking lot of time to process.
create table UnmatchedRecord
(select a.*
from HashedValues a
where a.Address_Hash not in(select b.Address_Hash
from HashAddress b)
and a.Person_Hash not in(select d.Person_Hash
from HashPerson d)
and a.HH_Hash not in(select f.HH_Hash
from HashHH f)
and a.VehicleRegistration not in(select VehicleRegistration
from MasterReference)
and a.EmailAddress not in (select EmailAddress
from MasterReference)
and a.PhoneNumber not in (select PhoneNumber
from MasterReference)
and a.NationalInsuranceNo not in (select NationalInsuranceNo
from MasterReference))
You can at least replace four subqueries with one:
select HashedValues.*
from HashedValues
where not exists (
select *
from MasterReference
where HashedValues.VehicleRegistration = MasterReference.VehicleRegistration
or HashedValues.EmailAddress = MasterReference.EmailAddress
or HashedValues.PhoneNumber = MasterReference.PhoneNumber
or HashedValues.NationalInsuranceNo = MasterReference.NationalInsuranceNo
)
and not exists (
select *
from HashAddress
where HashedValues.Address_Hash = HashAddress.Address_Hash
)
and not exists (
select *
from HashPerson
where HashedValues.Person_Hash = HashPerson.Person_Hash
)
and not exists (
select *
from HashHH
where HashedValues.HH_Hash = HashHH.HH_Hash
)

Laravel 4.2 execute query without using elequent query builder

I have a query with sub-query .. and i want to write an explicite SQL request and execute it
Query :
select count(*) as count from
(
SELECT DISTINCT t.article_id FROM
`articles` as a left join `tags` as t
on a.`id` = t.`article_id`
where a.`flag` = 1
) as sub
i tried this to execute the request without building the query :
DB::connection()->getPdo()->exec( $sql );
But it always return 0 !
You can use sub query with DB::raw.
A method DB::raw() (don’t forget to use Illuminate\Support\Facades\DB) allows you to select whatever you want and basically write raw SQL statements.
DB::select(DB::raw("select count(*) as count from
(
SELECT DISTINCT t.article_id FROM
`articles` as a left join `tags` as t
on a.`id` = t.`article_id`
where a.`flag` = 1
) as sub"));
https://laravel.com/docs/4.2/queries#raw-expressions
Why don't you try with DB::raw('your sql query here')
Using DB::select should solve your problem.
DB::select('select count(*) as count from
(
SELECT DISTINCT t.article_id FROM
`articles` as a left join `tags` as t
on a.`id` = t.`article_id`
where a.`flag` = 1
) as sub');

How to chain where and where not in in sql?

I have these two queries working:
var queryOne = client.query("SELECT * FROM tableone WHERE (tableone.id) NOT
IN ( SELECT tabletwo.id FROM tabletwo)");
var queryTwo = client.query("SELECT * FROM tableone WHERE time = 1")
I am attempting to combine them into one query:
var finalQuery = client.query("SELECT * FROM tableone WHERE time = 1 AND
WHERE (tableone.id) NOT IN ( SELECT tabletwo.id FROM tabletwo)");
However, I am running into this error: syntax error at or near "WHERE"
How can I correctly, combine the two queries?
drop the second WHERE, it's not needed:
var finalQuery = client.query("SELECT * FROM tableone WHERE time = 1 AND
(tableone.id) NOT IN ( SELECT tabletwo.id FROM tabletwo)");

Select Statement with INNER JOIN AND CASE

I'm trying to get sql statement to link from one table to another and use a case (or if) if the user has accesslevel 1 then the statement should be select * from campaigns, whereas if ur not accesslevel 1 u should get the select statement as :
select * from campaigns WHERE campaign_Creator = ${user_ID}
SELECT * FROM campaigns
INNER JOIN users ON campaigns.CAMPAIGN_CREATOR = users.USER_ID
CASE WHEN users.USER_ACCESSLEVEL = 1
THEN SELECT * FROM campaigns
ELSE select * from campaigns WHERE CAMPAIGN_CREATOR = ${user_ID};
How would I go around creating such a statement? I've been trying to look it up but i just get more confused as to how to go around with this.
Perhaps this is what you want?
SELECT * FROM campaigns
INNER JOIN users ON campaigns.CAMPAIGN_CREATOR = users.USER_ID
WHERE users.USER_ACCESSLEVEL = 1
OR CAMPAIGN_CREATOR = ${user_ID};
I agree with the solution of our friend and complementing follow my suggestion.
SELECT *
FROM campaigns
INNER JOIN users ON campaigns.CAMPAIGN_CREATOR = users.USER_ID
AND (users.USER_ACCESSLEVEL = 1 OR (users.USER_ACCESSLEVEL != 1
AND CAMPAIGN_CREATOR = ${user_ID}))

improving sql query in the case of many ORs

I have an SQL query which can have around 1.5k of IDs OR'ed in the following form. They are the IDs of people I have and they are individually selectable, hence the many ORs when everyone is selected. Is it better to just SELECT everyone's ID then put it in the AND condition, UNION instead of OR, or perhaps use IN to bunch up all IDs, since the user may select everyone accept 1 person. Thanks.
...
AND (
(UserID = '53b95690-22d8-44a2-ad56-919cb4037218')
OR (UserID = '87b7fc0c-28f4-4f2e-9909-066df42245fa')
OR (UserID = '98c1b5e3-6ba5-4bd9-b8f5-d3b2221e3e3a')
OR...
EDIT: The complete query, as requested;
SELECT *
FROM Mail WITH (NOLOCK)
WHERE Active= 1
AND Company= '1d034e8b-0122-4531-8795-895d9287920f'
AND (
CompanyDivisionID= '129bcca1-b1d8-4a9e-8152-0b9e936c9d01'
OR CompanyDivisionID= '1bf4023d-22a3-4520-b751-7842576f42b7'
)
AND (
(DestinationUserID = '53b95690-22d8-44a2-ad56-919cb4037218')
OR (DestinationUserID = '87b7fc0c-28f4-4f2e-9909-066df42245fa')
OR (DestinationUserID = '98c1b5e3-6ba5-4bd9-b8f5-d3b2221e3e3a')
...
...
...
OR (DestinationUserID = '8c78fc05-7969-48fd-9b30-774e5d9a70bd')
OR (DestinationUserID = 'e7b76096-fe7d-44b8-9158-8293ac609471')
OR (DestinationUserID = '8a6b7385-4339-43fb-b95b-a7b687982bcd')
)
ORDER BY SendingDate DESC
Create a (temporary) table containing your selected UserIDs and join to that table.
SELECT *
FROM Mail WITH (NOLOCK)
inner join SelectedUsers on mail.DestinationUserID = SelectedUsers.UserID
WHERE Active= 1