SQL - two tables retrieving data - php

i try to retrieve data but i think i'm not good at sql query.
i have a table eventCategory
and one more table
This two table is created in order to use in one table
this is the initplayer table.
What i want to do retrieve data like that
30191592 Izlesene.com 7 2012-02-22 izlesene_cihan_v4
How can i do that ? pls show me the way ? How can i use these two tables?

TRY
SELECT la.id,st.profileName, la.totalEvents,la.Date,ft.eventCategory
FROM lastTable la
INNER JOIN secondTable st ON st.id=la.id
INNER JOIN firstTable ft ON ft.id = la.eventCategoryID

You didn't say the name of the second table so I will just call it T2.
select *
from T2 join
eventCategory AS e on T2.eventCategoryID = e.id
I'm assuming your second table has an 'eventCategoryID' field. If it doesn't, then I would need to know how you would relate it to the other table.

Related

How to join two tables by 5 columns? Sql

I have two tables called customers and wantslist.
I want to join the columns customername, customeraddress , creditlimit , bytitle and byauthor.
How can I write a query that can show those column together in a table?
The first table is customers. It has the columns customeraddress , customername and creditlimit.
The second table is wantslist. It has the columns bytitle and byauthor.
How can I write a query that can join those 5 columns into 1 table?
Without knowing your specific strucutre you would use a SQL query similar to...
SELECT c.customername, c.customeraddress, c.creditlimit, w.bytitle, w. byauthor
FROM customers as c
JOIN wantslist as w on c.customerid = w.customerid
SELECT customers.customername, customers.customeraddress, customer.creditlimit, wantslist.bytitle, wantslist.byauthor
FROM wantslist
INNER JOIN customers
ON *{your matching condition}*;

Creating a table from query

Here is my 2 sql query on the first query i want to use it as a table and on the 2nd query i want to join the 1st query which i assume a table
e.g.
First query:
SELECT sale.salesManID, sale.companyName, sale.sellingPrice
from sale Inner Join carModel
On (sale.companyName = carModel.companyName) AND (carModel.size>10) ;
Second Query:
SELECT salesMan.salesmanID, salesMan.name, SUM(firstQuery.sellingPrice)
FROM salesMan
INNER JOIN firstQuery
ON salesMan.salesmanID = firstQuery.salesManID
GROUP BY salesmanID, name;
How can i do it ? Thanks in advance.
If I understand the question correctly (although I'm not sure why you'd want to do it this way...):
IF object_id('tempdb..#firstQuery) IS NOT NULL DROP TABLE #firstQuery;
SELECT sale.salesManID, sale.companyName, sale.sellingPrice
INTO #firstQuery
FROM sale Inner Join carModel On (sale.companyName =
carModel.companyName) AND (carModel.size>10) ;
SELECT salesMan.salesmanID, salesMan.name,
SUM(firstQuery.sellingPrice) FROM salesMan INNER JOIN #firstQuery ON
salesMan.salesmanID = firstQuery.salesManID GROUP BY salesmanID,
name;
it's not a clear question. do you mean using subquery on join???
maybe you can take a look to this threads:
mysql subquery inside a LEFT JOIN
MySQL correlated subquery in JOIN syntax
or just google mysql join subquery
also you can take a look this post: How can my application benefit from temporary tables?e

MySQL Inner Join involving three tables

I am working with four tables:
query,
store,
cluster_group,
tv_region
I want to retrieve query records belonging to a particular TV Region record.
A query record either belongs to a record in the store or cluster_group tables. The query table has 'store_id' and 'cluster_group_id' columns. Either will be null whilst the other will refer to a record in the store or cluster_group table. The store and cluster_group tables both have a 'tv_region_id' column.
To retrieve query records that belong to a TV Region record that has an id = 2, I wrote the following SQL statement:
SELECT query.id AS query_id, cluster_group.name as cluster_name, cluster_group.tv_region_id as cluster_tv_region, store.store_name
FROM query
INNER JOIN cluster_group
ON cluster_group.id=query.cluster_group_id
INNER JOIN store
ON store.id=query.store_id
WHERE cluster_group.tv_region_id = 2
AND store.tv_region_id = 2;
The problem is that it returns zero records even though there are query records that belong to the specified TV Region (via a cluster group or store record). I may have misunderstood how 'inner join' works.
I'm working with Doctrine 2 and have attempted the query using the left join but it still returns nothing, I guess this is because its returning null values too. How do I get it to return only the query records I am interested in and no null values?
Appreciate if someone can point me in the right direction to retrieve the relevant query records.
You will need to use OUTER joins, or possibly LEFT
INNER joins will only return data where there is a matching record on both sides. As you state in your question you only get a match on one side or the other, therefore there is never going to be a matching record across both store and cluster
Jeff Atwood has a good explanation of the join types on his blog http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html
Try this:-
SELECT query.id AS query_id, cluster_group.name as cluster_name, cluster_group.tv_region_id as cluster_tv_region, store.store_name
FROM query
LEFT OUTER JOIN cluster_group
ON cluster_group.id=query.cluster_group_id
AND cluster_group.tv_region_id = 2
LEFT OUTER JOIN store
ON store.id=query.store_id
AND store.tv_region_id = 2
EDIT - no experience of doctrine, but you could try using a UNION:-
SELECT query.id AS query_id, cluster_group.name as cluster_name, cluster_group.tv_region_id as cluster_tv_region, NULL AS store_name
FROM query
INNER JOIN cluster_group
ON cluster_group.id=query.cluster_group_id
AND cluster_group.tv_region_id = 2
UNION
SELECT query.id AS query_id, NULL as cluster_name, NULL as cluster_tv_region, store.store_name
FROM query
INNER JOIN store
ON store.id=query.store_id
AND store.tv_region_id = 2
It works like this for me:
from table1 inner join table2 on table1.id = table2.id
inner join table3 on table1.id = table3.id

SUM value from query changes when i add inner join to the query

$sql = mysql_query("SELECT totals.*, sum(totals.payments) as total_payments
FROM totals
INNER JOIN users
GROUP BY totals.idseller;");
When i add the INNER JOIN the sum value is changed. Why?
In my SQL table i have one record in totals width this value: 8943.09 but when i do the some the result is giving me this value: 44715.45
What i am doing wrong?
$sql = mysql_query("SELECT totals.*, sum(totals.payments) as total_payments FROM totals
INNER JOIN users ON totals.idseller = users.idseller
GROUP BY users.UserName;");
Use this Hope this will help you.
When you INNER JOIN to another table, the returned data set is modified to only include rows that exist in both tables. In this case it is likely that there are rows in 'totals' that do not have a matching row in users - either the totals.idseller field might accept null values, or data has become orphaned when matching users have been deleted or edited.
If you want all data in 'totals' regardless of matching user you would user a LEFT JOIN instead in ms-sql, I suspect a similar approach will work in my-sql
You should give an "on" based on the ids. Such as like
inner join users on users.id = totals.idseller
Otherways the sql server will combine all possible rows in the tables, which is most cases not what you wish.
Because when you are adding inner join in your SQL Query, it means you are selecting the data which is common in both the tables.
EX:
SELECT * FROM TABLE_A
INNER JOIN TABLE_B
ON TABLE_A.ID = TABLE_B.ID
If you are joining users table which contains 5 records. By joining table, as there is no any column mapping, this sum-up 5 times and this is reason for showing different values.
Please let me know something wrong in it.
Thanks,
Umehs

Struggling with SQL

I have two table in the same database: hlstats_Events_Connects and hlstats_PlayerUniqueIds.
In the hlstats_Events_Connects I have a value I wish to get, however the ID it's related to I need to get from the data in another table, using the "uniqueId" that I have. Example:
**hlstats_Events_Connects**
playerId eventTime
----------------------
8 2013-04-05 05:44:14
**hlstats_PlayerUniqueIds**
playerId uniqueId
---------------------
8 0:0:84901
So I have the "uniqueId" as a variable, and I want to say, get the playerId of the persons uniqueId, then get the eventTime from what the playerId is. Currently I have something along the lines of the below, but can't figure out the Where clause.
SELECT c.eventTime, c.playerId, u.uniqueId, u.playerId
FROM c.hlstats_Events_Connects, u.hlstats_PlayerUniqueIds
WHERE ...?
Cheers
Looks like you are just missing the JOIN. You will join the tables on the playerId column:
SELECT c.eventTime, c.playerId, u.uniqueId, u.playerId
FROM c.hlstats_Events_Connects c
INNER JOIN u.hlstats_PlayerUniqueIds u
ON c.playerId = u.playerId
Note, I updated the query to use ANSI JOIN syntax, in this case an INNER JOIN. This is standard SQL syntax, instead of commas between the tables and the join in the WHERE
Use JOIN like this
SELECT c.eventTime, c.playerId, u.uniqueId, u.playerId
FROM c.hlstats_Events_Connects, u.hlstats_PlayerUniqueIds
WHERE c.playerId = u.playerId
You don't need to add prefixes to table names, instead those should be specified after table name, and also you can just join on playerId column (if those are large tables, I'd suggest adding an index to those columns.)
SELECT
c.eventTime, c.playerId, u.uniqueId, u.playerId
FROM
hlstats_Events_Connects c,
hlstats_PlayerUniqueIds u
WHERE
u.uniqueId = "0:0:84901" and
u.playerId = c.playerId

Categories