Select from two tables with INNER JOIN not functioned as expect - php

I need a little help setting up my query. I'm simply trying to get the users from table advertise_jobs who have the same ID in the table applicants or in another meaning I need to get the users who have applied on a job from the advertise_jobs by the id.
this is my query
SELECT DISTINCT applicants.*, advertise_jobs.*, uid
FROM applicants
INNER JOIN advertise_jobs
ON applicants.ads_id=advertise_jobs.id
GROUP BY applicants.ads_id
it's not functioned as expect it gets other jobs that the users didn't apply to
how can I fix my query to be something like
select * from applicants and * from advertise_jobs where applicants.ads_id = uid

I think this will work
SELECT DISTINCT applicants.uid, advertise_jobs.ads_id
FROM applicants,advertise_jobs
WHERE applicants.uid=advertise_jobs.uid
GROUP BY applicants.ads_id

Related

how can i get group table name and also get am i member or not in that group

I am working on a project, there has group feature. when I search any group, this time I have to know that am I already join those listed group.
I have group table and another table group user table for adding the user to the group.
I want to get it with SQL query only with one query. How can I do it?
You could try an inner join
SELECT *
FROM Groups
INNER JOIN UserGroup on Groups.Group_ID = UserGroup.Group_ID
I don't know exactly your tables,but it should be something like that.

SQL query for Selecting from Multiple Tables in single database

I am having 3 tables (c19 , c19b2, g26) in a database
I want to write a SQL Query to search and display all fields of the matched record.
I am using following query:
$query = "SELECT * FROM c19,c19b2,g26 WHERE armyno LIKE '%$searchTerm%'";
But it only works for table c19,
Data from the other 2 tables is not fetched.Each table has a field armyno
Please help me with this
Thank you.
Alright, you are not looking for a JOIN, but a UNION.
SELECT * FROM c19 WHERE armyno LIKE '%$searchTerm%'
UNION
SELECT * FROM c19b2 WHERE armyno LIKE '%$searchTerm%'
UNION
SELECT * FROM g26 WHERE armyno LIKE '%$searchTerm%'
That will let you query all three tables at the same time.
Which DB are you using? This would have worked in SQL Server. However, notice you are doing a cross join of every record to every record... usually you only want to match some records by restriction of a matching key, for example:
select
*
from a
left join b on b.somekey = a.somekey
left join c on c.someotherkey = b.someotherkey
In SQL server you can just say *, but I'm taking it that in your DB engine that didn't work, so try specifying which table. This may in some environments require aliasing as well:
select
a.*,
b.*,
c.*
from tableA as a
left join tableB as b on b.somekey = a.somekey
left join tableC as c on c.someotherkey = b.someotherkey
Generally, you should see the columns from the first table, followed by the columns from the second table, followed by columns from the third table for a given row. If you wanted to get all columns from all tables, but separately, then that would be 3 separate selects.
Lastly, if all 3 tables have "armyno" then I'd expect it to throw an ambiguous field error. In such case you'd want to specify which table's "armyno" field to filter on.

Getting user id from on table and counting them in another table

I have to make a scores page for a game and one thing it has to display is the number of times the current user has played the game. Basically what i need to select ID and username from one table, link them together and count the corresponding user IDs from another table.
These are the tables I was given: Users --> ID, USERNAME and Times_Played --> ID, USER_ID
Thanks in advance
You want to query something like this:
SELECT Users.USERNAME, Times_Played.User_ID
FROM Users
INNER JOIN Times_Played
ON Users.ID=Times_Played.ID;
EDIT
After re-reading the question and seeing the need to count the times played you could do this via PHP or via your SQL Query. Via PHP:
$result = mysqli_query(
"SELECT Users.USERNAME, Times_Played.User_ID
FROM Users
INNER JOIN Times_Played
ON Users.ID=Times_Played.ID"
);
$timesPlayed = mysqli_num_rows($result);
If it has to be done via the query, Anish has the correct solution.
I hope that helps.
Try this
select count(*) as Times_Played,Users.USERNAME,Users.ID
from Users
inner join Times_Played
on Users.ID = Times_Played.USER_ID

mySQL logic: output of table join not correct

I am trying to query two tables: finished_events and flagged_events. 1st of all I need everything related to the company_id so
SELECT *
FROM finished_events
WHERE company_id=$id
ORDER by schedule, timestamp
I then changed this to:
SELECT * FROM finished_events
INNER JOIN flagged_events
ON finished_events.company_id=flagged_events.company_id
WHERE finished_events.company_id=$id
ORDER by finished_events.schedule, finished_events.timestamp
I have tried using FULL JOIN, LEFT JOIN, and RIGHT JOINs all unsuccessful. Specifically what I want is to get is a combined effort of the following code:
$sql = "SELECT *
FROM finished_events
WHERE company_id=$id
ORDER by schedule, time_stamp";
$flagged_sql = "SELECT *
FROM flagged_events
WHERE company_id=$id
ORDER by schedule, time_stamp";
The tables are a bit different so UNION won't work here. I can post dummy database entries but this won't be of too much help as I need all from both tables. The 2 links between the tables would be the company_id and the schedule columns. Essentially what is going on behind the scenes is timestamps being put into a different table to which I then process either into finished_events or flagged_events. Flagged events will need the user to do something about it until it is a finished event. So this script is generating the data for the GUI, hence why I need to query both tables and create an associative array of customer details then an array of events (from these 2 tables). So creating the assoc_array is no problem I just need to get this query to spit out all the events and order them correctly. Let me know if you need anything specific to solve this one, thanks :)
EDIT
SQL Fiddle: http://sqlfiddle.com/#!2/d4c30/1
this almost fixes it but not quite right, it repeats entries at the bottom
If I understood correctly, this may be useful for you:
SELECT a.* FROM (
SELECT *, 'finished' as event_type FROM finished_events
UNION
SELECT *, 'flagged' as event_type FROM flagged_events) a
ORDER BY a.schedule, a.time_stamp

cross referencing 2 tables and getting results

I have 2 mysql tables
one has a list of user ids that are associated with a city. ie "Fort Lauderdale" but the user id is actually in a column called entity_id and the city is in a field called field_city_value.
This query brings back all of the entity_ids in "Fort Lauderdale"
SELECT entity_id
FROM `field_data_field_city`
WHERE `field_city_value` LIKE 'Fort Lauderdale'
and then this query brings back the mail for the user id
SELECT mail
FROM `users`
WHERE `uid` =42
I want to combine the 2 and get all of the mails for all of the user ids that match Fort Lauderdale.
Use a join statement.
http://dev.mysql.com/doc/refman/5.0/en/join.html
Heres the long winded way of doing it.. Untested, no mysql access on this box.
SELECT field_data_field_city.entity_id,users.mail FROM users,field_data_field_city WHERE field_city_value LIKE 'Fort Lauderdale' AND field_data_field_city.entity_id = users.uid
Or
SELECT * FROM field_data_field_city city INNER JOIN users user on city.entity_id=user.uid
Can we assume that users.uid is the same as field_data_field_city.entity_id? If that's the case you'll want to look into using MySQL joins
You can try a subselect:
SELECT mail FROM users WHERE uid IN (SELECT entity_id FROM field_data_field_city WHERE field_city_value LIKE 'Fort Lauderdale')
This should do it. You might want to change it a little bit for duplicates or similar, but this simple query should work
SELECT u.mail
FROM users u
LEFT JOIN field_data_field_city fdfc ON fdfc.entity_id = u.uid
WHERE fdfc.field_city_value LIKE 'Fort Lauderdale'

Categories