Need help in a sql query - php

I have a field that is a varchar that contain values such as (88,90,100,200) and i have another one contains the value 200 when using the IN clause to see if the second field in the first field it returns empty result but when comparing it with 88 it return results
So i was wondering what im doing wrong here and if there is a better way.
Here is the mysql code
select * from user inner join category where parent_id IN (categories)
parent_id is located in the category table and the categories in the user table

When you do a JOIN you need an ON clause to specify that you are looking for rows that somehow "match" across two tables. If you want all the rows from a single table where some column has a value that is "one of the values in this list..." you use IN. So these are both valid SQL:
SELECT * FROM user INNER JOIN category ON user.someColumn = category.someOtherColumn // can add WHERE clause
or
SELECT * FROM user WHERE parent_id IN (88, 99, 100, 200)
Can't tell you exactly what query you should use unless you share your table structures, as the question is unclear.

Related

CodeIgniter join select as

I have 2 tables in my database which I need to join.
1 table is the artikelen table and the other one is the collecties table. I currently have.
$this->db->select('*');
$this->db->from('collecties');
$this->db->join('artikelen', 'artikelen.collecties_id = collecties.id');
It gives the right result but all the double fields (collecties has a title field and artikelen has a title field) will become one (it returns the artikelen.title field), and I can't access the row of the other table (the collecties.title field).
I select 10 fields from artikelen and only collecties.title from collecties.
What is the simples way to do this without having to replace
$this->db->select('*');
with all the 10 fields with an as statement.
Make sure your both table got rows on your joining condition , otherwise it will return null. and modify the select as follows
$this->db->select('artikelen.*,collecties.title as ctitle');

mysql join two tables likes and posts

Here is my php/MySQL task:
I have a table POSTS that contains num field that is the primary key and other information fields about the post (author, title, etc.). I also have a table LIKE that contains a userId field that is the primary key and a field POST that corresponds to the num field in posts. Given a specific userID, I need to get all of the rows from the POSTS table that the userId 'likes'.
Table 1 - posts
-num
-author
-title
Table 2 - likes
-userId
-postId
This is all in php so my first idea was to get all of the rows from the LIKES table where the userId matches the one given and store those rows in an array. Then I would iterate through the array and for each row I would search get the row of the POSTS table where postId=POSTS.num. However, this seems like it would be rather slow, especially since each iteration through the array would be a separate mysql query.
I am assuming there is a faster way. Would it be to use a temporary table or is there a better way to join the tables? I have to assume that both tables contain many rows. I am a mysql novice so if there is a better solution please explain why it is better. Thank you in advance for you help!
Try the following query:
SELECT
`posts`.*
FROM
`likes`
INNER JOIN
`posts` ON
`posts`.num = `likes`.postId
WHERE
`likes`.Userid = {insert user id here}
Depending on your schema (not sure if each record in 'likes' has to be unique, you may want to use the DISTINCT keyword on your select to filter out duplicates.
SELECT poli.* FROM (
SELECT po.* FROM posts po
JOIN likes li
ON li.postId = po.num
WHERE li.userId = '$yourGivenUserId'
) AS poli
$yourGivenUserId is the given userId.

MySQL Join table and get results that don't exist in one

I have two tables, one that has a foreign key from the other. I want to get all records that don't exist in the foreign table, based on certain criteria.
Here are the tables I have:
item_setting
setting_id
category_id
item
item_id
setting_id
name
expired_dt
Here's the query I'm using now:
SELECT
iset.setting_id
FROM
item_settings iset
LEFT OUTER JOIN
item i ON i.setting_id = iset.setting_id
WHERE
iset.category_id = '5' AND i.setting_id is null
This query works in providing any setting_id's that do not have a record in the item's table within a specific category.
However, now I want to include cases where the expired_dt less than than time() (meaning it's past expired). In otherwords, I would think to add this:
WHERE
iset.category_id = '5' AND (i.setting_id is null OR i.expired_dt < '".time()."')
However, this doesn't work, it returns all the records.
Any suggestions? Maybe I'm completely over complicating this.... I just want to return the setting_id's from the item_settings table, where the expired_dt associated in the item table is expired or if it does not even exist in the item table.
Thank you!
Try moving the timestamp condition into the join clause. Something like
item_settings iset
LEFT OUTER JOIN
item i ON i.setting_id = iset.setting_id and i.expired_dt > time()

changing during accomplish query

I've such a interested question for me , please if anyone know answer me .
So I have two table , in first i have column named "num" which includes (like say phone numbers) and in second table i have column "sec_num" which also includes some of the exactly same numbers like in first table from column "num" and second column named "persons" (from second table also)
I am retrieving data from first table and i want that if first table phone numbers (from num column) matched to second table numbers (from column "sec_num") then retrieved value from "persons" column. Is it possible to do ? if yes , please help me , Thanks ...
PS. if it is necessary to know i am using php, but I think this job must do sql
If I understand the question right a regular left join should do the trick;
select num, persons from first left join second on num=sec_num;
If we call the tables tableA (first table) and tableB (second table) thern a query like:
SELECT * FROM tableA INNER JOIN tableB on tableA.num = tableB.sec_num
should do the trick.

php mysql compare two columns return mismatches

I have two columns in different product tables.
tblproduct1.partno is an old product list
tblproduct2.partno2 is a new one
Both partno columns should have identical model numbers but they don't.
When executing the below query, I get about 300 model numbers that don't match when comparing counts from both tables. tblproduct2 has 1955 records, the query below is 1638. I would expect it to return 1955.
SELECT COUNT(partno)
FROM tblproduct1
INNER JOIN tblproduct2 ON partno = partno2
Is there a way I can list the model numbers that don't match?
select tblproduct1.partno from tblproduct1
left join tblproduct2 on tblproduct1.partno = tblproduct2.partno2
where tblproduct2.partno2 is null
shows tblproduct1.partno that have no matching tblproduct2.partno2 values
Actually stereofrogs query is correct. It works even when the table columns are defined as 'not null' I suspect you had the two tables mixed up when you ran the query.
that is because the LEFT JOIN always has all the rows from the left table in it. If the second table does not have a matching entry it will be displayed as NULL.
So as long as you have the table with more rows as the left (or the first) table the above query will produce the desired result.

Categories