I'm trying to use the INNER JOIN functionality in my phpMyAdmin.
My query looks like this:
SELECT * FROM ___Bookings INNER JOIN ___Origins ON ___Bookings.BOO_Origin=___Origins.ORI_Id WHERE BOO_Id=1.
The problem is at this step nothing is populated into the ___Origins table. So my query returns 0 row.
How to change my query to return a row even if I do not have the joined table populated ?
Also what's the difference between JOIN and INNER JOIN ?
Thanks so much.
Basically, you want to join the tables based on the data in the __BOOKINGS table. That's a job for LEFT JOIN, not INNER JOIN (which is the same as JOIN).
Refer here for more information on SQL Joins: http://www.sql-join.com/sql-join-types/
There isn't a difference between Join and Inner Join (you can search for it also if this isn't enough).
For the other part - how can it return a row when there isn't one?
If you need a response from PHP you can set something like
if ($query->rowCount() > 0) {
echo $records;
}
else {echo "N";}
And in you other code just state if the response is "N" (or something else) do something or not do anything.
Related
I need to create a view on top of a table which contains 1300+ columns. New data will be loaded to table every quarter(Rows in millions). While creating view I need to join other table with the base table. and i also needed to add a recent row indicator in view.
CREATE OR REPLACE SECURE VIEW VIEW_NAME AS
SELECT lkp_tbl.col1,base_tbl.col1,base_tbl.col2,base_tbl.col3,........,
base_tbl.col1334, 1 as Is_Latest_Quarter
FROM base_tbl full outer JOIN lkp_tbl
on base_tbl.CUST_ID = lkp_tbl.CUST_ID
where snapshot_dt=(select max(snapshot_dt) from base_tbl)
union all
SELECT lkp_tbl.col1,base_tbl.col1,base_tbl.col2,base_tbl.col3,........,
base_tbl.col1334,0 as Is_Latest_Quarter
FROM base_tbl full outer JOIN lkp_tbl
on base_tbl.CUST_ID = lkp_tbl.CUST_ID
where snapshot_dt!=(select max(snapshot_dt) from base_tbl);
After creating this view the performance of the query is too slow even if we are querying 100 rows. Is there a way in which we can create view in more efficient way. If not how can i increase performance?
just use one SELECT statement and use a CASE statement to calculate Is_Latest_Quarter
UPDATED WITH (ALMOST) ACTUAL SQL
CREATE OR REPLACE SECURE VIEW VIEW_NAME AS
SELECT {list of columns you want to include}
,CASE WHEN snapshot_dt=(select max(snapshot_dt) from base_tbl) THEN 1
ELSE 0 END as Is_Latest_Quarter
FROM base_tbl
full outer JOIN lkp_tbl on base_tbl.CUST_ID = lkp_tbl.CUST_ID
Alternatively, if Snowflake doesn't like that in-line subquery, your could use a CTE something like:
CREATE OR REPLACE SECURE VIEW VIEW_NAME AS
WITH MAX_DATE AS (SELECT MAX(Ssnapshot_dt) AS max_snapshot_dt FROM base_tbl),
SELECT {list of columns you want to include}
,CASE WHEN max_date.max_snapshot_dt is not null THEN 1
ELSE 0 END as Is_Latest_Quarter
FROM base_tbl
full outer JOIN lkp_tbl on base_tbl.CUST_ID = lkp_tbl.CUST_ID
LEFT OUTER JOIN MAX_DATE ON base_tbl.snapshot_dt = max_date.max_snapshot_dt
I am joining tbl_A to tbl_B, on column CustomerID in tbl_A to column Output in tbl_B which contains customer ID. However, tbl_B has all other information in related rows that I do not want to lose when joining. I tried to join using like, but I lost rows that did not contain customer ID in the output column.
Here is my join query in Hive:
select a.*, b.Output from tbl_A a
left join tbl_B b
On b.Output like concat('%', a.CustomerID, '%')
However, I lose other rows from output.
You could also achieve the objective by a simple hive query like this :)
select a.*, b.Output
from tbl_A a, tbl_B b
where b.Output like concat('%', a.CustomerID, '%')
I would suggest first extract all ID's from free floating field which in your case is 'Output' column in table B into a separate table. Then join this table with ID's to Table B again to populate in each row the ID and then this second joined table which is table B with ID's to table A.
Hope this helps.
I am having to convert code written by a former employee to work in a new database. In doing so I came across some joins I have never seen and do not fully understand how they work or if there is a need for them to be done in this fashion.
The joins look like this:
From Table A
Join(Table B
Join Table C
on B.Field1 = C.Field1)
On A.Field1 = B.Field1
Does this code function differently from something like this:
From Table A
Join Table B
On A.Field1 = B.Field1
Join Table C
On B.Field1 = C.Field1
If there is a difference please explain the purpose of the first set of code.
All of this is done in SQL Server 2012. Thanks in advance for any help you can provide.
I could create a temp table and then join that. But why use up the cycles\RAM on additional storage and indexes if I can just do it on the fly?
I ran across this scenario today in SSRS - a user wanted to see all the Individuals granted access through an AD group. The user was using a cursor and some temp tables to get the users out of AD and then joining the user to each SSRS object (Folders, reports, linked reports) associated with the AD group. I simplified the whole thing with Cross Apply and a sub query.
GroupMembers table
GroupName
UserID
UserName
AccountType
AccountTypeDesc
SSRSOjbects_Permissions table
Path
PathType
RoleName
RoleDesc
Name (AD group name)
The query needs to return each individual in an AD group associated with each report. Basically a Cartesian product of users to reports within a subset of data. The easiest way to do this looks like this:
select
G.GroupName, G.UserID, G.Name, G.AccountType, G.AccountTypeDesc,
[Path], PathType, RoleName, RoleDesc
from
GroupMembers G
cross apply
(select
[Path], PathType, RoleName, RoleDesc
from
SSRSOjbects_Permissions
where
Name = G.GroupName) S;
You could achieve this with a temp table and some outer joins, but why waste system resources?
I saw this kind of joins - it's MS Access style for handling multi-table joins. In MS Access you need to nest each subsequent join statement into its level brackets. So, for example this T-SQL join:
SELECT a.columna, b.columnb, c.columnc
FROM tablea AS a
LEFT JOIN tableb AS b ON a.id = b.id
LEFT JOIN tablec AS c ON a.id = c.id
you should convert to this:
SELECT a.columna, b.columnb, c.columnc
FROM ((tablea AS a) LEFT JOIN tableb AS b ON a.id = b.id) LEFT JOIN tablec AS c ON a.id = c.id
So, yes, I believe you are right in your assumption
By default, when you perform query to sphinx table, Sphinx engine returns rows which are already sorted by query weight and does it really fast.
So, when I do this:
select
article.name
from article
left join article_ft on article._id=article_ft.id
where article_ft.query='some text;mode=any;';
Where:
article is InnoDB like table.
article_ft is Sphinx table.
Both of them (article.name and article_ft) contain these data (1 line = 1 row):
This is text.
This is also some text.
This is another text.
Sphinx engine will return rows like:
This is also some text.
This is text.
This is another text.
But, If I do something like this:
select
article.name
from article
left join article_ft on article._id=article_ft.id
left join article_category on article.category=article_category._id
where article_ft.query='some text;mode=any;';
It seems, MariaDB sorts it by its own way here.
Even If I provide Sphinx's 'sort' option like this:
select
article.name
from article
left join article_ft on article._id=article_ft.id
left join article_category on article.category=article_category._id
where article_ft.query='some text;mode=any;sort=extended:#weight desc;';
Still it doesn't work.
Changing order of joins doesn't work as well.
If I use order by article_ft.weight DESC MariaDB returns error message like:
Error: ER_ILLEGAL_HA: Storage engine SPHINX of the table `article_ft` doesn't have this option
in case if article has no rows that could match condition like article.category=50.
article_ft was created using this:
CREATE TABLE article_ft
(
id BIGINT NOT NULL,
weight INTEGER NOT NULL,
query VARCHAR(3072) NOT NULL,
INDEX(query)
) ENGINE=SPHINX CONNECTION="sphinx://192.168.1.98:9402/article_ft";
How to use this "magical" sort by weight feature if query contains more joins with no errors in return?
Thanks forward, for any reply!
P.S. Can't provide you a fiddle for this because I do not know any SQL fiddle online service which supports Sphinx Tables. Also if you found more relevant topic question I'll appreciate that.
Put the article_ft table first in the query. ie ... article_ft inner join article ...
Or maybe use FORCE INDEX, to force the use of the query index. Then it might honour the sort order.
Failing that use a subquery?
(select name,weight from article_ft ... ) order by weight desc;
I am hoping someone can shed some light to this error I am receiving on a Query script ...
SELECT
...
FROM
LEFT OUTER JOIN DATESA ON DAILY_DATES.ID = DATESA.ID
table3,
table4
WHERE
...
I am getting an error message and stuck; can't figure out the issue on my syntax?!
Why do you put table3, table4 after the join? You specified the tables already in the join
SELECT ...
FROM daily_dates
LEFT OUTER JOIN datesa
ON daily_dates.id = datesa.id
WHERE ...
And you forgot to specify your first table after the select (view my example for the syntax)